Nginx負載均衡_1

Nginx負載均衡_1

  1. 3臺及以上Nignx server服務器

    192.168.0.1  (負載均衡服務器)
    192.168.0.2  (WEB 服務器1)
    192.168.0.3  (WEB 服務器2)
  2. 負載均衡服務器配置

        gzip on;
    
        #均詢式負載均衡
        upstream www.svrap.com {
            server 192.168.0.2:80;
            server 192.168.0.3:80;
        }
    
        #權重式負載均衡
        #upstream www.svrap.com {
        #    server 192.168.0.2:80 weight=10;
        #    server 192.168.0.3:80 weight=10;
        #}
    
        #ip_hash負載均衡(session穩定)
        #upstream www.svrap.com {
        #    ip_hash;
        #    server 192.168.0.2:80;
        #    server 192.168.0.3:80;
        #}
    
        #fair負載均衡(第三方)(響應最快服務器優先分配給用戶)
        #upstream www.svrap.com {
        #    server 192.168.0.2:80;
        #    server 192.168.0.3:80;
        #    fair;
        #}
    
        #url_hash負載均衡(第三方)(後端服務器爲緩存時效果較好)
        #upstream www.svrap.com {
        #    server 192.168.0.2:80;
        #    server 192.168.0.3:80;
        #    hash $request_uri;
        #    hash_method crc32;
        #}
    
        #upstream中server格式:
        #server ip:port [down|weight=?|max_fails|fail_timeout|backup];
        #down:         表示單前的server暫時不參與負載
        #weight:       默認爲1.weight越大,負載的權重就越大。
        #max_fails:    允許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
        #fail_timeout: max_fails次失敗後,暫停的時間。
        #backup:       其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
    
        server {
            listen       80;
            server_name  www.svrap.com; 
    
            charset utf-8;
    
            access_log  /nginx_access.log  main;
    
            location / {
                proxy_pass     http://www.svrap.com;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
            error_page 403 404 /40x.html;
            location = /40x.html {
                root html;
            }
    
            error_page 404 500 502 503 504 /50x.html;
            location = /50x.html {
                root html;
            }
        }
  3. 注意事項

    nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
    client_body_in_file_only: 設置爲On 可以講client post過來的數據記錄到文件中用來做debug
    client_body_temp_path:    設置記錄文件的目錄 可以設置最多3層目錄
    location:                 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章