最近在做项目中需要给前端提供文件下载链接,所以借着机会搭建了基于Nginx的文件服务器,特此记录便于日后查阅。
1、配置文件
需要修改 nginx.conf 配置文件(内网地址:10.12.1.215、外网地址:113.98.58.42),内容如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
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;
proxy_buffering off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
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;
# Load configuration files for the default server block.
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/;
# proxy_cookie_path / /ts/api;
}
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;
# Load configuration files for the default server block.
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;
# Load configuration files for the default server block.
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;
# Load configuration files for the default server block.
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 {
}
}
}
注意:这里有几个在文件上传时候的优化点,内容如下:
proxy_request_buffering off; //禁用请求缓冲(不加的话大文件上传可能会在前端报415)
proxy_buffering off; //禁用缓冲(不加的话大文件上传可能会在前端报415)
client_max_body_size 500M; //限制上传文件大小最大500MB
2、验证
将文件上传到 /usr/local/oas/file/upload/sas/auth/ 目录下(文件名为 20200303111734279),然后点击以下链接下载文件到本地,内容如下:
http://113.98.58.42:9010/upload/sas/auth/20200303111734279
或者你也可以直接访问服务器文件所在目录地址,内容如下:
http://113.98.58.42:9010/
访问结果,图示如下:
到此 Nginx 配置服务器文件访问及下载介绍完成。