nginx配置proxy_pass代理转发时报404问题

2024-03-01 0 309
目录

nginx配置proxy_pass代理转发时报404

加 / 与不加 /

在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径

例如:

  • 加 /

server_name xxx.com
location /data/ {
proxy_pass http://127.0.0.1/;
}

访问 http://xxx.com/data/index.xml 会转发到 http://127.0.0.1/index.html

  • 不加 /

server_name xxx.com
location /data/ {
proxy_pass http://127.0.0.1;
}

访问 http://xxx.com/data/index.xml 会转发到 http://127.0.0.1/data/index.html

nginx配置proxy_pass转发的/路径问题

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;

如果没有/,则会把匹配的路径部分也给代理走。

location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}

如上面的配置,如果请求的url是http://servername/static_js/test.html

会被代理成http://js.test.com/test.html

而如果这么配置

location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}

则会被代理到http://js.test.com/static_js/test.htm

当然,我们可以用如下的rewrite来实现/的功能

location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)/1 break;
proxy_pass http://js.test.com;
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Nginx服务器 nginx配置proxy_pass代理转发时报404问题 https://www.u-9.cn/server/nginx/174584.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务