Nginx读书笔记
... upstream proxy_svrs { server http://192.168.1.1:8001/uri/; server http://192.168.1.2:8001/uri/; server http://192.168.1.3:8001/uri/; } server { server_name www.myweb.com; location / { proxy_pass proxy_svrs; } } ...
在使用proxy_pass指令的过程中需要注意如果在upstream中的服务器中都指明了传输协议“http://”,而在proxy_pass指令中就不需要指明了。如果现在将upstream指令的配置改为:
upstream proxy_svrs { server 192.168.1.1:8001/uri/ server 192.168.1.2:8001/uri/; server 192.168.1.3:8001/uri/; }
就需要在proxy_pass指令中指明传输协议“http://” proxy_pass http://proxy_svrs;
在使用时还需要注意,URL中是否包含有URI,Nginx处理方式是不同的。如果URL中不包含URI,Nginx服务器不会改变原地址的URI,但是如果包含了URI,Nginx服务器将会使用新的URI代替原来的URI。
负载均衡示例配置:
... upstream videobackend { server 192.168.1.2:80; server 192.168.1.3:80; server 192.168.1.4:80; } upstream filebackend { server 192.168.1.5:80; server 192.168.1.6:80; server 192.168.1.7:80; } server { listen 80; server_name www.myweb.com; index index.html index.htm; location /video/ { proxy_pass http://videobackend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /file/ { proxy_pass http://filebackend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ...