因项目前后端分离,前端和后端在两台服务器上,所以文件上传之后的访问需在两台服务器上都安装nginx,如下:
- A服务器:前端服务器(开放外网)
- B服务器:服务端服务器(未开发外网)
- 需求:A服务器访问B服务器的文件
只需在A服务器上配置反向代理,转发到B服务器即可;具体配置如下: - A服务器nginx配置:
#给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配
location ^~ /files/{
try_files $uri @new_uploads;
}
location @new_uploads{
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;
proxy_pass http://B服务器内网IP:9001;
}
- B服务器的配置:
server {
listen 9001;
server_name localhost;
location ^~ /files/{
alias /home/file/;
#autoindex on;
index index.html index.htm;
}
}
重启nginx服务后
访问:A服务器IP:端口/files/xxx
即可访问到B服务器的/home/file/
目录下的文件了(和访问http://B服务器内网IP:9001/files/
的效果是一样的)。
参考:
https://blog.csdn.net/Answer0902/article/details/122996146