项目上使用负载均衡的方法, 感觉最简单的办法其实是 http的 upstream 注意需要保留端口号信息.
worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream webservers { sticky; server 10.3.110.60:5200; server 10.3.110.61:5200; } server { listen 8081; server_name localhost; root /usr/share/nginx/html; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_set_header Host $host:8081; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header 'Access-Control-Allow-Credentials' "true"; proxy_pass http://webservers; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
注意 listen的端口号 需要与后面的 host的端口号一致起来