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命令不生效

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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