![nginx proxy_pass 无法提供静态文件](https://linux22.com/image/742130/nginx%20proxy_pass%20%E6%97%A0%E6%B3%95%E6%8F%90%E4%BE%9B%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6.png)
我一直在尝试实现以下目标,但几天来一直认为失败了。
我正在尝试按照以下步骤根据 URL 路径反向代理 2 服务:
http://host/api/ => http://backend/
http://host/backoffice/ => http://backoffice/
(并且之后的所有路径都应由服务 1 和 2 承载,即
http://host/service1/foo/bar => http://server1/foo/bar
)
当我访问非静态文件时,重定向效果很好(服务 1 是响应的 rest API),但当访问静态资源时,我遇到了以下问题(服务 2 是由 nginx 服务器提供服务的 React 应用程序)。反向代理指示 css 和 js 文件出现 404。
代理的配置如下:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
tcp_nopush on;
tcp_nodelay on;
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_set_header X-Forwarded-Host $server_name;
server {
listen 80;
listen 443;
server_name reverse-proxy;
location /backoffice/ {
proxy_pass http://backoffice:80/;
rewrite /backoffice/(.*) /$1 break;
}
location /api/ {
proxy_pass http://backend:8000/;
rewrite /backend/(.*) /$1 break;
}
}
}
查看反向代理的日志,我读到以下内容:
172.31.39.227 - - [03/Sep/2019:08:37:44 +0000] "GET
/static/js/2.741f53a0.chunk.js HTTP/1.1" 404 572 "http://host/backoffice/" "Mozilla/5.0 (X11;
Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/74.0.3729.169 Safari/537.36"
2019/09/03 08:37:44 [error] 7#7: *669 open()
"/etc/nginx/html/static/js/2.741f53a0.chunk.js" failed (2: No such
file or directory), client: <host-ip>, server: reverse-proxy,
request: "GET /static/js/2.741f53a0.chunk.js HTTP/1.1", host:
"host", referrer:
"http://host/backoffice/"
2019/09/03 08:37:44 [error] 7#7: *671 open()
"/etc/nginx/html/static/js/main.cf227c52.chunk.js" failed (2: No such
file or directory), client: <host-ip>, server: reverse-proxy,
request: "GET /static/js/main.cf227c52.chunk.js HTTP/1.1", host:
"host", referrer:
"http://host/backoffice/"
<server ip> - - [03/Sep/2019:08:37:44 +0000] "GET
/static/js/main.cf227c52.chunk.js HTTP/1.1" 404 572 "http://host/backoffice/" "Mozilla/5.0 (X11;
Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/74.0.3729.169 Safari/537.36"
(我在该日志中更改了主机名和 IP)
我最初根据端口侦听器进行了重定向,效果很好,但对于 DNS 目的,我不能依赖该解决方案。我做错了什么?