1. 程式人生 > >Nginx原始碼編譯安裝及實現負載均衡

Nginx原始碼編譯安裝及實現負載均衡

Nginx (engine x)

是一個高效能的HTTP和反向代理服務,工作在網路的7層之上,可以針對http應用做一些分流的策略, 比如針對域名、目錄結構,它的正則規則比HAProxy更為強大 和靈活,這也是它目前廣泛流行的主要原因之一,Nginx單憑 這點可利用的場合就遠多於LVS了。Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好. 

實驗環境:

iptables和selinux關閉

1.作業系統:rhel6.5

2.主機:

物理主機:172.25.254.68

server2 : 172.25.254.2 作主節點,為了提供Cong配置使用者界下載ricci,luci

Server3: 172.25.254.3 下載ricci,作副節點

一.nginx的安裝:

此操作在server2和server5上均做

vim /etc/yum.repos.d/rhel-source.repo

server2:

tar zxf nginx-1.14.0.tar.gz

cd nginx-1.14.0

vim src/core/nginx.h  #把版本號隱藏

vim auto/cc/gcc

# debug

#CFLAGS="$CFLAGS -g"   #註釋這行

yum install gcc openssl-devel -y  pcre-devel  #安裝一些依賴性

原始碼編譯三步:

第一步生成makefile 第二步讀取檔案生成的二進位制檔案 第三步開始安裝

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio

make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    #製作軟連結

nginx的基本語法: 

nginx -t  檢測語法

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

nginx -s stop  關閉服務

nginx  開啟服務 

nginx -s reload 過載服務

測試:

curl -I localhost

瀏覽器測試:

二.負載均衡

server2:

把server2的CPU核數改為2

lscpu

cd /usr/local/nginx/conf/

vim nginx.conf

useradd -M -d /usr/local/nginx/ -u 800 nginx  #nginx建立使用者預設組也為nginx

id nginx

nginx

nginx  -t  #檢查語法錯誤

nginx  -s reload

vim /etc/security/limits.conf

負載均衡配置:

vim /usr/local/nginx/conf/nginx.conf  編輯主配置檔案

nginx -t

nginx -s reload

server3和server4:

/etc/init.d/httpd start

測試:

curl www.westos.org

三.健康檢查

vim /usr/local/nginx/conf/nginx.conf

nginx -t

nginx -s reload

yum install -y httpd

vim /etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80

Listen 8080

/etc/init.d/httpd restart

vim  /var/www/html/index.html

<h1>網站維護中.......</h1>

server3和server4:

/etc/init.d/httpd stop

真機測試:

更改權重:

Server2:

vim /usr/local/nginx/conf/nginx.conf

http {        

upstream westos{        

server 172.25.2.3:80 weight=2;        

server 172.25.2.4:80;        

server 172.25.2.2:8080 backup;        

}

nginx -t

nginx -s reload

測試:

手動下線server3:

Server2:

vim /usr/local/nginx/conf/nginx.conf

nginx -t

nginx -s reload

測試:

curl localhost

Sticky cookie:

Sticky工作原理 Sticky是nginx的一個模組,通過分發和識別 cookie,來使同一個客戶端的請求落在同一臺伺服器上。sticky 的處理過程如下(假設cookie名稱為route):

1.客戶端首次發起請求,請求頭未帶route的cookie。nginx接收請求, 發現請求頭沒有route,則以輪詢方式將請求分配給後端伺服器。

2.後端伺服器處理完請求,將響應頭和內容返回給nginx。

3.nginx生成route的cookie,返回給客戶端。route的值與 後端伺服器對應,可能是明文,也可能是md5、sha1等Hash值。

4.客戶端接收請求,並建立route的cookie。

5.客戶端再次傳送請求時,帶上route。 6.nginx接收到route,直接轉給對應的後端伺服器

cd /mnt

unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip

cd nginx-1.14.0

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/mnt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42

make && make install

cd /usr/local/nginx/conf/

vim nginx.conf

真機測試:

在瀏覽器測試:www.westos.org  瀏覽器才有cookie,curl命令不生效