废话不多说直接简单测试
# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; access_log /var/log/nginx/log/host.access.log accsee; location / { root /data/web/www; index index.html index.htm; } location /image { proxy_pass http://192.168.2.46:8080; } } # vim /etc/nginx/conf.d/test.conf server { listen 8080; server_name localhost; access_log /var/log/nginx/log/host.test.log accsee; location / { root /data/web/test; index index.html index.htm; } } 目录结构 tree /data/web/ /data/web/ ├── test │ ├── image │ │ └── index.html │ └── index.html └── www └── index.html # cat /data/web/test/index.html [8080] # cat /data/web/test/image/index.html [image] # cat /data/web/www/index.html [80]
浏览器访问:http://192.168.2.46/image
修改配置文件:
# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; access_log /var/log/nginx/log/host.access.log accsee; location / { root /data/web/www; index index.html index.htm; } location /image { proxy_pass http://192.168.2.46:8080/; #后面加上 “/” } } 重启 nginx 服务
总结:
当加上了"/",是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
如果没有"/",则会把匹配的路径部分也给代理走。