美文网首页
Nginx proxy_pass 后出现部分请求 404 的解决

Nginx proxy_pass 后出现部分请求 404 的解决

作者: awker | 来源:发表于2018-11-13 01:22 被阅读0次

有一个后端服务,地址是 http://127.0.0.1:8888/web-test/

nginx 配置如下

    location ^~ /web-test/ { 
        proxy_pass http://127.0.0.1:8888;
    }

访问 http://ip:port/web-test/是正常的

但我想改写成访问 http://ip:port/test/ 替代访问 http://ip:port/web-test/
nginx 配置如下

location ^~ /test/ { 
    proxy_pass http://127.0.0.1:8888/web-test/;
}

重写后访问 http://ip:port/test/后出现问题,看到请求下面的 静态资源URL 都返回 404,也就是说页面的静态资源发起的请求还是访问了原来的 /web-test 路由

http://127.0.0.1:8888/web-test/resource/... 
http://127.0.0.1:8888/web-test/image/...
http://127.0.0.1:8888/web-test/system/...
http://127.0.0.1:8888/web-test/其他 URI/...

这种情况 nginx 要怎样配置重写规则?访问 http://ip:port/test/能返回正常的请求

解决办法:
1、

location ^~ /test/ { 
    proxy_pass http://127.0.0.1:8888/web-test/;
}

其他 静态资源URI 请求也 proxy_pass 到后端服务

location /web-test/resource/ {
    proxy_pass http://127.0.0.1:8888;
}
location /web-test/image/ {
    proxy_pass http://127.0.0.1:8888;
}
location /其他静态资源URI请求 {
    proxy_pass http://127.0.0.1:8888;
}

这种方式要找到所有的 静态资源URL 请求,一个个重写

2、
直接修改后端服务路由为 http://127.0.0.1:8888/test/,规则变为

location ^~ /test/ { 
    proxy_pass http://127.0.0.1:8888;
}

就可以了

相关文章

网友评论

      本文标题:Nginx proxy_pass 后出现部分请求 404 的解决

      本文链接:https://www.haomeiwen.com/subject/erahfqtx.html