Nginx反向代理和負載均衡部署指南

1.        安裝

1)         從Nginx官網下載頁面(http://nginx.org/en/download.html)下載Nginx最新版本(目前是1.5.13版本)安裝包;

2)         解壓後複製到部署目錄。

 

2.        啓動和停止Nginx

Nginx目前只支持命令行操作,操作前先進入Dos命令環境,並進入Nginx部署目錄。

1)         啓動Nginx:start nginx

2)         停止Nginx:nginx -s stop

3)         修改配置後重啓:nginx -s reload

這三個命令可分別做成bat文件,放在部署目錄下,方便後續操作。

start nginx.bat文件內容:start nginx

stop nginx.bat文件內容:nginx -s stop

reload nginx.bat文件內容:nginx -s reload

 

3.        反向代理配置

修改部署目錄下conf子目錄的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)內容,可調整相關配置。

反向代理配置示例:

location / {

        #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實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;

 

             #禁用緩存

             proxy_buffering off;

 

             #設置反向代理的地址

             proxy_pass http://192.168.1.1;       

      }

代理地址根據實際情況修改。

 

4.        負載均衡配置

nginx 的 upstream默認是以輪詢的方式實現負載均衡,這種方式中,每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。

另外一種方式是ip_hash:每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。 

 

負載均衡配置示例:

upstream backend {

             #ip_hash;

 

             server 192.168.1.251;

             server 192.168.1.252;

             server 192.168.1.247;

         }

 

server {

        listen       80;

        server_name  trffweb;

 

        location / {

 

             #反向代理的地址

             proxy_pass http://backend;     

        }

}

 

Upstream命名和服務器地址根據實際情況修改。

 

5.        完整配置示例

nginx.conf:

 

worker_processes  1;

events {

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 

    upstream backend {

             #ip_hash;

             server 192.168.1.251;

             server 192.168.1.252;

             server 192.168.1.247;

         }

 

    server {

        listen       80;

        server_name  2;

 

        location / {

        #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實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;

 

             #禁用緩存

             proxy_buffering off;

 

             #反向代理的地址

             proxy_pass http://backend;     

        }

    }

 

}

6.示例


環境:

nginx1:192.168.68.41

tomcat1:192.168.68.43

tomcat2:192.168.68.45

nginx安裝網上很多教程,我是用yum安裝的。

配置nginx:

vim /etc/nginx/conf.d/default.conf 

內容:

#負責壓縮數據流
gzip              on;  
gzip_min_length   1000;  
gzip_types        text/plain text/css application/x-javascript;

#設定負載均衡的服務器列表
#weigth參數表示權值,權值越高被分配到的機率越大
upstream hello{
    server 192.168.68.43:8080 weight=1;
    server 192.168.68.45:8080 weight=1;            
}
   
server {
    #偵聽的80端口
    listen       80;
    server_name  localhost;
    #設定查看Nginx狀態的地址
    location /nginxstatus{
         stub_status on;
         access_log on;
         auth_basic "nginxstatus";
         auth_basic_user_file htpasswd;
    }
    #匹配以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服務器傳
    }
}


啓動:

nginx

退出:

nginx -s quit

啓動nginx後,訪問http://192.168.68.41/test/

會被導航到http://192.168.68.43:8080/test/ 和http://192.168.68.45:8080/test/

從而實現了負載均衡和避免單點故障。

 

////////////////////////////////
////////Sixi. Let it be.../////
//////////////////////////////


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章