今天在配置nginx的时,碰到的一个问题。
先来看下面的正确配置
upstream backend_122{ server 192.168.80.122 weight=5 max_fails=3 fail_timeout=30s; } upstream backend_122_02{ server 192.168.80.122:15672; } server{ listen 80; server_name 122.100.com; access_log /var/log/nginx/122.100.com.access.log main; error_log /var/log/nginx/122.100.com.error.log; location ^~ /rabbit/ { #这里也最好带上/ proxy_pass http://backend_122_02/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Remote_addr $remote_addr; } location / { proxy_pass http://backend_122; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Remote_addr $remote_addr; } }
注意上面2处标识出来的代码的区别,一个最末尾加了 / ,一个没有 / ,这2个时有区别的
举例子说明吧:
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.html