1. 程式人生 > >nginx 做負載均衡,炒雞簡單

nginx 做負載均衡,炒雞簡單

nginx 負載均衡

第一步,在nginx 配置 nginx.conf 的 http 指令中建立upstream

upstream backend_https {
    server  10.2.20.80:443 weight=1 max_fails=2 fail_timeout=10s;
    server  10.2.20.69:443 weight=1 max_fails=2 fail_timeout=10s backup;
    sticky;
}

upstream backend_https_h5 {
    server  10.1.20.69:3000 weight=1 max_fails=2
fail_timeout=10s; server 10.1.20.80:3000 weight=1 max_fails=2 fail_timeout=10s backup; sticky; }

第二步,修改對應的網站設定,做負載指向

location ~/
{
    proxy_pass https://backend_https_h5;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    include   proxy.conf;
}

第三步,重啟 nginx

nginx -s reload

負載均衡注意session問題 及程式獲取真實IP問題。
真實IP的傳遞可以使用 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

檔案 proxy.conf

proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
; #proxy_set_header X-Forwarded-For $upstream_addr; proxy_set_header Connection ""; proxy_http_version 1.1; client_max_body_size 30m; client_body_buffer_size 128k; #proxy_connect_timeout 3; #proxy_send_timeout 10; #proxy_read_timeout 10; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 32k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_next_upstream error timeout http_500 http_502 http_503 http_504;