最近,测试环境误删,此功能开发人员离职,我临时救火接手了文件转发的功能。
对于文件功能,实际的开发中经常会遇到,一般有2中实现方式,1种是使用稳定的文件分布式服务器,即OSS(Object Storage Service)服务,对于一般的公司来说,起点比较高;另外一种就是使用服务器的目录作为临时存储,这种实现方式很不稳定,文件有被误删的可能,Nginx便是实现文件转发的一个利器。
其中nginx需要添加nginx_upload_module;使用nginx -V检查即可;
其主要配置如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log; #默认错误日志存储位置
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
#标识http协议的一些日志格式和数据类型
http {
include /root/nginx/conf/mime.types;
default_type application/octet-stream;
#日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #需要打开
keepalive_timeout 50;
#gzip压缩设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 8;
gzip_types text/plain image/jpeg image/gif image/png application/octet-stream;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
upstream testservice { #服务器集群名字,此处叫做 test-service
server 192.168.10.1:7890; #服务器配置 weight是权重的意思,权重越大,分配的概率越大。
}
#nginx服务器的监听ip和端口,使用的编码格式
server {
listen 80;
server_name 192.168.10.1 ; #监听IP
charset utf-8;
#上传配置
location /upload {
client_max_body_size 50m;
upload_pass /xxx/xxx; # 表示Nginx接收完上传的文件后,然后交给后端处理的地址
upload_store /tmp/nginx-upload 1; #//上传模块接收到的文件临时存放的路径, 1 表示方式,该方式是需要在/tmp/nginx-upload下创建以0到9为目录名称的目录,上传时候会进行一个散列处理。
upload_pass_args on;
upload_store_access user:rw; # 上传文件的权限,rw表示读写 r只读
upload_set_form_field "${upload_field_name}Name" $upload_file_name; # 这里写入http报头,pass到后台页面后能获取这里set的报头字段
upload_set_form_field "${upload_field_name}ContentType" $upload_content_type;
upload_set_form_field "${upload_field_name}SavePath" $upload_tmp_path;
upload_aggregate_form_field "${upload_field_name}Md5" $upload_file_md5; # Upload模块自动生成的一些信息,如文件大小与文件md5值
upload_aggregate_form_field "${upload_field_name}Size" $upload_file_size;
upload_aggregate_form_field "${upload_field_name}Sha1" $upload_file_sha1;
#upload_pass_form_field "^submit$|^description$"; # 允许的字段,允许全部可以 "^.*$"
upload_pass_form_field "^.*$";
upload_limit_rate 0; # 每秒字节速度控制,0表示不受控制,默认0
upload_cleanup 400 404 499 500-505; # 如果pass页面是以下状态码,就删除此次上传的临时文件
}
#下载配置
location /file {
# internal; #内部调用
alias /tmp/nginx-upload;
#add_header Content-Disposition attachment;
#add_header Content-Type application/octet-stream;
}
#nginx的常用配置
location / { #location代表拦截的请求路径/标识全部拦截
proxy_pass http://testservice; #固定格式标识负载到哪个upstream上,这里选择之前配置的test-service上.即http://test-service=http://+test-service
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 3;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 128k;
proxy_buffers 4256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
# redirect server error pages to the static page /50x.html
#错误页面的跳转到哪些html上在nginx目录当中都是有的
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注:文件下的服务请求,应先到nginx,对于文件的基本信息,需要通过NG的转发,到后端的服务器获取。