想通过 http://aaa/ceshi 访问到 http://bbb 的内容, 但是代理后一些 js ,css 文件报 404,发现是缺少前缀的问题。请问 a 机器上怎么配置? 谢谢大家
location ^~/ceshi/ {
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-NginX-Proxy true;
proxy_pass http://192.168.0.50;
}
location ^~/ceshi/ {
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-NginX-Proxy true;
proxy_pass http://localhost/;
}
[08/Apr/2022:11:22:46 +0800] "GET / HTTP/1.0" 127.0.0.1 - -200 26867 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36" "192.168.21.233, 192.168.0.53"
:81/js/chunk-f0487752.5a2a9024.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
:81/js/chunk-f5dfb222.b4892263.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
:81/js/chunk-f46a5f00.d5bf17fb.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
:81/js/chunk-fc9277e6.a8e8a87f.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
:81/js/chunk-ffb40534.0d082972.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
iview.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
gitalk.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
quill.snow.css:1
1
julyclyde 2022-04-08 12:31:08 +08:00
|
2
skiy 2022-04-08 12:41:11 +08:00
原因是因为你网站请求的静态资源(前缀)不包含 /ceshi/ ,所以它没进代理里面。
你还需要另外再设置一个 (.css | .js ) 的伪静态。 |
3
adoal 2022-04-08 12:54:23 +08:00
因为后端的应用并不具备“意识到自己有可能运行在前缀路径下”的能力,所以动态页面里生成的静态资源链接还是按照自己运行在根路径下的。
要解决这个问题,要么改造后端程序加入这种探测能力。要么(不一定能 100%完美准确判定)在 nginx 里加入改写规则,对后端返回的页面里静态资源的绝对路径做改写加上前缀。 |
4
adoal 2022-04-08 13:04:49 +08:00
比如说用 Python Flask 写的可以参照这个例子 https://gist.github.com/jrialland/bf017843bbead538c20c382a19d880fe
其它语言和框架应该有类似 trick |
5
rekulas 2022-04-08 13:10:03 +08:00
<base>标签强制制定下根路径试试
如果还不行只能改造输出代码了,没有 100%完美解决方案 |
6
luxu OP |
7
wolfmei 2022-04-10 21:47:02 +08:00
试下 location /ceshi {proxy_pass http://192.168.0.50;}
如果不行,可以加个 proxy_redirect 试下 |