nginx負載均衡

Nginx (engine x) 是一個高性能的HTTP和反向代理服務器,也是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。
Nginx作爲負載均衡服務器優點:
1.Nginx 既可以在內部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作爲 HTTP2.代理服務器對外進行服務。
2.理靜態文件,索引文件以及自動索引;打開文件描述符緩衝。
3.無緩存的反向代理加速,簡單的負載均衡和容錯。
4.FastCGI,簡單的負載均衡和容錯。
5.模塊化的結構。
配置環境: 操作環境:rhel6.5
調度器:172.25.40.5 server5.example.com
服務器:172.25.40.2 server2.example.com
172.25.40.3 server3.example.com
客戶端本地解析:
vim /etc/hosts
172.25.40.5 www.westos.org westos.org bbs.westos.org

Ngin 源碼安裝

tar zxf nginx-1.12.0.tar.gz
cd nginx-1.12.0
隱藏版本號:
cd nginx-1.12.0/src/core/
vim nginx.h
#define NGINX_VER          "nginx"
取消debug
cd /root/nginx-1.12.0/auto/cc
vim gcc
#debug
#CFLAGS="$CFLAGS -g"
cd nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx負載均衡

Nginx 四種方式的負載均衡配置:
1.基於輪詢(Round-Robin)方式的負載均衡:

cd /usr/local/nginx/conf
vim nginx.conf
user  nginx nginx;     
worker_processes  2;
worker_cpu_affinity 01 10;   綁定cpu
events {
    worker_connections  65535;       #併發連接數
}
http {
upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        }
 server{
                listen 80;
                server_name www.westos.org;
                location / {
                        proxy_pass http://westos;
                }

        }
}

在配置併發連接數時,修改/etc/security/limits.conf文件後纔會生效。

 vim /etc/security/limits.conf
nginx           -       nofile          65535

測試頁面:
nginx負載均衡

nginx負載均衡

關閉server2 server3端的httpd服務,瀏覽器頁面自動調轉到調度器本地nginx服務端。

http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        Server172.25.40.5:80 backup;
        }
}

nginx負載均衡

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        server localhost:8080 backup;
        }
        server{
                listen 8080;
                server_name localhost:8080;
                charset utf-8;
                location / {
                        root /backup;
                        index index.html;
                }

        }
}

nginx負載均衡

2.基於權重方式的負載均衡
配置權重:

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80 weight=2;
        server 172.25.40.3:80;
        server localhost:8080 backup;
        }
}

nginx負載均衡
3.基於ip_hash方式的負載均衡:使用長連接,確保一個客戶只和一臺服務器通信

vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        ip_hash;
        }
}

測試結果:
nginx負載均衡

4.使用nginx sticky實現基於cookie的負載均衡
Sticky 模塊
cp /usr/local/nginx/conf/nginx.conf /opt/
重新安裝nginx,因當前版本過高,不能添加sticky模塊,現在安裝nginx-1.10.1版本。
執行前面安裝的步驟,這裏不再說明。需要注意的是在編譯時要添加--add-module=PATH參數,模塊路徑是解壓的nginx-sticky-module-ng.tar.gz的解壓路徑。

cd /root/nginx-1.10.1
 ./configure --help --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-ng
make && make install
cp /opt/nginx.conf /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
http {
        upstream westos{
        sticky;
        server 172.25.40.2:80;
        server 172.25.40.3:80;
        }
}

nginx -t #檢測語法
nginx -s reload #重新加載服務
注意:
1.nginx和apache不同,nginx每次安裝一個新的模塊都需要重新編譯一次,編譯完成之後將nginx這一個文件拷貝到sbin下面即可。
2.如果在reload時出現下面報錯時,解決辦法是:
nginx負載均衡

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