1. 程式人生 > >【Nginx】配置Nginx的負載均衡

【Nginx】配置Nginx的負載均衡

如果 tail 運行 connect 其他 least label direct doc

閱讀目錄

  • 參考的優秀文章
  • 在本機運行2個Tomcat
  • Nginx的負載均衡配置

參考的優秀文章

  1. tomcat配置文件server.xml詳解
  2. AJP協議總結與分析
  3. Using nginx as HTTP load balancer

在本機運行2個Tomcat

現需要運行兩個Tomcat,監聽不同端口,讓Nginx作負載均衡跳轉過來。
Tomcat版本:apache-tomcat-7.0.69-windows-x64.zip

要在一臺機器運行兩個Tomcat,要解決端口的沖突,我們只需要D:\green\tomcat_cluster\apache-tomcat-7.0.69-server-1\conf\server.xml

portredirectPort的端口修改成系統不占用的不同端口即可。

  1. <Server port="8005" shutdown="SHUTDOWN">, 關閉服務器監聽的端口。
  2. <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />, port="8080",HTTP連接監聽的端口;redirectPort="8443",遇到SSL請求,將請求轉跳的端口。
  3. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    ,AJP監聽的端口和遇SSL轉跳的端口。關於AJP,我也不懂,請參考:AJP協議總結與分析

Nginx的負載均衡配置

Nginx版本:nginx-1.10.1.zip

Nginx支持好幾種形式的均衡分配:

  • 循環(round-robin)
  • 最少連接(least-connected)
  • IP哈希(ip-hash)
  • 權重(weight)

其中,IP哈希的負載均衡是具有粘性的,就是會話中同一客戶端的請求始終會分配給一臺服務器,如果沒有實現Session共享的,可以使用IP哈希。

以下是最簡單的循環的配置方式:
加了以下配置:

http {
    ......
    upstream myweb_proxy {
        server 127.0.0.1:18080;
        server 127.0.0.1:28080;
    }
    ......
    server {
        ......
        location /myweb {
            proxy_pass http://myweb_proxy;
        }
        ......
    }
}

其他配置,參考:Using nginx as HTTP load balancer

【Nginx】配置Nginx的負載均衡