本文共 4376 字,大约阅读时间需要 14 分钟。
在项目开发过程中,为了方便前端团队获取服务器端文件,搭建了基于Nginx的文件服务器。以下是搭建过程及相关配置记录。
Nginx配置文件位于/etc/nginx/nginx.conf,以下是核心配置内容:
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;events { worker_connections 1024;}http { 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 /var/log/nginx/access.log main; proxy_request_buffering off; # 禁用请求缓冲(不加的话大文件上传可能会在前端报415) proxy_buffering off; # 禁用缓冲(不加的话大文件上传可能会在前端报415) client_max_body_size 500M; # 限制上传文件大小最大500MB sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /usr/share/nginx/modules/*.conf; include /etc/nginx/conf.d/*.conf; server { listen 9010; server_name localhost; charset utf-8; include /etc/nginx/default.d/*.conf; location / { root /usr/local/oas/file; sendfile on; autoindex on; autoindex_exact_size off; autoindex_localtime on; } } server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; client_max_body_size 500M; include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/PaaS/; try_files $uri /index.html; index index.html; } location /ts/api { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://10.12.1.206:31001/; } location /ts/others { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://10.12.1.206:31001/others; } location /oas-cloud { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://10.12.1.215:30103/oas-cloud; } location /zuul { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://10.12.1.215:30103/zuul; } location /tengine { proxy_pass http://127.0.0.1:8080/; } location /uum { proxy_pass http://127.0.0.1:8081/; } location /cdc { proxy_pass http://127.0.0.1:8082/; } location /upload { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://127.0.0.1:9010/upload; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 8080; server_name localhost; client_max_body_size 500M; include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/tengine/; try_files $uri /index.html; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 8081; server_name localhost; client_max_body_size 500M; include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/UUM/; try_files $uri /index.html; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 8082; server_name localhost; client_max_body_size 500M; include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html/CDC/; try_files $uri /index.html; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }} 将文件上传至/usr/local/oas/file/upload/sas/auth/目录下(文件名为20200303111734279),然后通过以下链接下载文件到本地:
http://113.98.58.42:9010/upload/sas/auth/20200303111734279
通过访问服务器文件所在目录地址:
http://113.98.58.42:9010/
文件上传优化
配置中设置了以下优化参数,确保文件上传过程顺畅:proxy_request_buffering off; # 禁用请求缓冲proxy_buffering off; # 禁用缓冲client_max_body_size 500M; # 限制上传文件大小
访问地址
服务器地址及端口信息:通过以上配置及验证步骤,Nginx文件服务器已成功搭建完成,支持文件下载及相关服务访问。
转载地址:http://smcfk.baihongyu.com/