nginx在配置proxy_pass的时候 URL结尾加斜线(/)与不加的区别和注意事项
假设访问路径的 /manager/index.html
加/斜线的情况
location /manager/ {
proxy_pass http://127.0.0.1:8096/;
}
被代理的真实访问路径为:http://127.0.0.1:8096/index.html
不加/斜线的情况
location /manager/ {
proxy_pass http://127.0.0.1:8096;
}
被代理的真实访问路径为:http://127.0.0.1:8096/manager/index.html
当我们代理web服务器的时候,假如
location /manager/ {
proxy_pass http://127.0.0.1:8096/;
}
本地访问项目是http://127.0.0.1:8096/login,代理访问项目地址是http://127.0.0.1/manager/login。
访问http://127.0.0.1/manager/login页面是没有问题的,但是login页面下的静态文件就加载不了,因为它访问的路径是http://127.0.0.1/static/css/login.css,原因是反向代理的路径下找不到文件,需要单独指定js css文件的访问路径。因为这个时候项目加载静态文件的时候
location ~ .*\.(js|css)$ {
proxy_pass http://127.0.0.1:8096;
}
另外一种解决办法就是项目加访问前缀manager,这个时候本地访问的是http://127.0.0.1:8096/manager/login,静态资源http://127.0.0.1:8096/manager/static/css/login.css,那么代理修改为:
location /manager/ {
proxy_pass http://127.0.0.1:8096/manager/;
}
这个时候代理访问的是http://127.0.0.1/manager/login,静态资源访问的是:http://127.0.0.1/manager/static/css/login.css,那么就访问成功了。