Windows 通過Nginx實現負載均衡和反向代理配置
阿新 • • 發佈:2018-12-06
Nginx版本安裝地址 http://nginx.org/en/download.html
找到nginx.conf檔案,修改兩個地方(在安裝檔案conf資料夾中)
新增伺服器池,設定2臺伺服器,權重級別都設定2
#設定負載均衡的伺服器列表
#upstream表示負載伺服器池,定義名字為hello 的伺服器池
#weigth引數表示權值,權值越高被分配到的機率越大
upstream hello {
server localhost:8080 weight=2;
server localhost:8090 weight=2;
}
修改nginx埠,預設80,本地測試容易發生衝突,我們設定8899
#監聽埠
server {
listen 8899;
server_name localhost;
實現負載均衡配置,當一臺伺服器掛起,會跳到另外一臺伺服器
#匹配以jsp結尾的,tomcat的網頁檔案是以jsp結尾 location / { index index.jsp; proxy_pass http://hello; #在這裡設定一個代理,和upstream的名字一樣 #以下是一些反向代理的配置可刪除 proxy_redirect off; #後端的Web伺服器可以通過X-Forwarded-For獲取使用者真實IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允許客戶端請求的最大單檔案位元組數 client_body_buffer_size 128k; #緩衝區代理緩衝使用者端請求的最大位元組數 proxy_connect_timeout 300; #nginx跟後端伺服器連線超時時間(代理連線超時) proxy_send_timeout 300; #後端伺服器資料回傳時間(代理髮送超時) proxy_read_timeout 300; #連線成功後,後端伺服器響應時間(代理接收超時) proxy_buffer_size 4k; #設定代理伺服器(nginx)儲存使用者頭資訊的緩衝區大小 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設定 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定快取資料夾大小,大於這個值,將從upstream伺服器傳 }
測試結果