1. 程式人生 > >linux-Centos-7-64位:5、 nginx代理+sticky模組實現基於cookie的負載均衡

linux-Centos-7-64位:5、 nginx代理+sticky模組實現基於cookie的負載均衡

nginx 可以使用各平臺的預設包來安裝,本文是介紹使用原始碼編譯安裝,包括具體的編譯引數資訊。

準備工作:

1、安裝必備工具:

編譯環境gcc g++ 開發庫之類的需要提前裝好

$ yum -y install gcc gcc-c++ autoconf automake

安裝過程中如果出現如下圖錯誤:

這裡寫圖片描述

原因:
dns伺服器沒有配置正確
解決辦法:
①、開啟檔案/etc/resolv.conf在其中新增:

 nameserver 8.8.8.8
 nameserver 8.8.4.4
 search localdomain

這裡寫圖片描述

②、然後重啟網絡卡:使用命令: service network restart

如果是在虛擬機器配置,那要把路由設定成和主機一致

這裡寫圖片描述

$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

說明:

pcre: 用來作地址重寫的功能。

zlib:nginx 的gzip模組,傳輸資料打包,省流量(但消耗資源)。

openssl:提供ssl加密協議。

也可以先線上下下載:

2、新建一個系統級使用者組和匿名使用者,以及下面編譯時使用

$ sudo groupadd -r nginx
 
$ sudo useradd -s /sbin/nologin -g nginx -r nginx-user

檢視使用者組及其成員

groups nginx-user

這裡寫圖片描述

Nginx編譯安裝:

在 usr/local/src 目錄下建立 nginx 資料夾

mkdir nginx

將下載好的 nginx-1.9.15.tar.gz 放到 /mnt/share資源目錄並複製到 /usr/local/src/nginx 資料夾下並解壓,並進入解壓後的nginx-1.9.15資料夾下
nginx-sticky-module-1.1.tar.gz 的做法也一樣

cp /mnt/share/nginx-sticky-module-1.1.tar.gz /usr/local/nginx/
tar -zxvf nginx-sticky-module-1.1.tar.gz
mv nginx-sticky-module-1.1.tar.gz nginx-sticky-module-up

注意事項 --> nginx-1.9.x和nginx-sticky-module-1.1編譯會有點問題,應該是nginx新版本沒有相應的nginx-sticky-module,解決辦法是把nginx-sticky-module-up/ngx_http_sticky_misc.c的281行修改為:
digest->len = ngx_sock_ntop(in,sizeof(struct sockaddr_in), digest->data, len, 1);

cd nginx-sticky-module-up
vi ngx_http_sticky_misc.c

顯示行號

:set number

這裡寫圖片描述
然後再編輯 nginx-sticky-module-up/ngx_http_sticky_module.c
第6行新增

#include <nginx.h>

第340行新增

#if defined(nginx_version) && nginx_version >= 1009000
		iphp->rrp.current = peer;
#else
 		iphp->rrp.current = iphp->selected_peer;
#endif			

這裡寫圖片描述
接下來解壓 nginx-1.9.15

cp /mnt/share/nginx-1.9.15.tar.gz /usr/local/nginx/
tar -zxvf nginx-1.9.15.tar.gz
cd nginx-1.9.15/

這裡寫圖片描述

./configure 引數配置資訊詳情,可以通過 --help 查詢

$ ./configure \					#nginx啟動
--prefix=/usr/local/nginx \				# 設定安裝目錄,預設使用 /usr/local/nginx。
--sbin-path=/usr/local/nginx/nginx \	# 設定nginx的可執行檔案的路徑,預設為prefix/sbin/nginx.
--conf-path=/etc/nginx/nginx.conf \		# 設定在nginx.conf配置檔案的路徑。nginx允許使用不同的配置檔案啟動,通過命令列中的-c選項。預設為prefix/conf/nginx.conf.
--error-log-path=/var/log/nginx/error.log \		# 設定主錯誤,警告,和診斷檔案的名稱。安裝完成後,可以隨時改變的檔名 ,在nginx.conf配置檔案中 使用 的error_log指令。預設情況下,檔名 為prefix/logs/error.log.
--http-log-path=/var/log/nginx/access.log \		# 設定主請求的HTTP伺服器的日誌檔案的名稱。安裝完成後,可以隨時改變的檔名 ,在nginx.conf配置檔案中 使用 的access_log指令。預設情況下,檔名 為prefix/logs/access.log.
--pid-path=/var/run/nginx.pid \			# 設定nginx.pid檔案,將儲存的主程序的程序號。安裝完成後,可以隨時改變的檔名 , 在nginx.conf配置檔案中使用 PID指令。預設情況下,檔名 為prefix/logs/nginx.pid.
--lock-path=/var/run/nginx.lock \		# 設定lock檔案(nginx.lock)目錄
--http-client-body-temp-path=/var/cache/nginx/client_temp \	# 設定客戶端請求臨時檔案路徑
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \		# 設定http proxy臨時檔案路徑
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \	# 設定http fastcgi臨時檔案路徑
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \		# 設定路徑儲存http uwsgi臨時檔案路徑
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \			# 設定路徑儲存http scgi臨時檔案路徑
--user=nginx \		#  設定nginx工作程序的使用者。安裝完成後,可以隨時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的使用者名稱是nobody。
--group=nginx \		# 設定nginx工作程序的使用者組。安裝完成後,可以隨時更改的名稱在nginx.conf配置檔案中 使用的 user指令。預設的為非特權使用者。
--with-http_ssl_module \		# 使用https協議模組。預設情況下,該模組沒有被構建。建立並執行此模組的OpenSSL庫是必需的。
--with-http_realip_module \		# 允許ngx_http_realip_module模組(mod_rpaf)
--with-http_addition_module \	# ..模組(mod_layout)
--with-http_sub_module \		# ..模組
--with-http_dav_module \		# ..模組(mod_dav)
--with-http_flv_module \		# ..(mod_flvx)
--with-http_mp4_module \		# ..模組(mod_mp4)
--with-http_gunzip_module \		# ..模組(mod_gunzip)
--with-http_gzip_static_module \		# ..模組(mod_dflate)
--with-http_random_index_module \		# ..模組(mod_autoindex)
--with-http_secure_link_module \		# ..模組
--with-http_stub_status_module \		# ..模組(mod_status)通過web介面檢視時Nginx需要開啟status模組
--with-http_auth_request_module \		# 允許ngx_http_auth_request_module模組
--with-http_xslt_module 		# 允許ngx_http_xslt_module模組
--with-mail \		# 允許POP3/IMAP4/SMTP代理模組
--with-mail_ssl_module \		# 允許ngx_mail_ssl_module模組
--with-file-aio \	# 允許檔案AIO支援
--with-ipv6 \		# 允許IP6代理支援
--with-http_spdy_module \  	# 允許ngx_http_spdy_module模組1.9.5開始要把把--with-http_spdy_module換成--with-http_v2_module,不然報錯
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'  # 設定C編譯器引數
--without-http_charset_module 		# 不使用ngx_http_charset_module模組
--without-http_gzip_module 			# 不使用ngx_..模組
--without-http_ssi_module 			# 不使用ngx_..模組
--without-http_userid_module 		# 不使用ngx_..模組
--without-http_access_module 		# 不使用ngx_..模組
--without-http_auth_basic_module 	# 不使用ngx_..模組
--without-http_autoindex_module 	# 不使用ngx_..模組
--without-http_geo_module 			# 不使用ngx_..模組
--without-http_map_module 			# 不使用ngx_..模組
--without-http_referer_module 		# 不使用ngx_..模組
--without-http_rewrite_module 		# 不使用ngx_..模組
--without-http_proxy_module 		# 不使用ngx_..模組
--without-http_fastcgi_module 		# 不使用ngx_..模組
--without-http_memcached_module 	# 不使用ngx_..模組
--without-http_limit_zone_module 	# 不使用ngx_..模組
--without-http_empty_gif_module 	# 不使用ngx_..模組
--without-http_browser_module 		# 不使用ngx_..模組
--without-http_upstream_ip_hash_module 		# 不使用ngx_..模組
--with-http_perl_module 		# 允許ngx_..模組
--with-perl_modules_path=PATH 	# 設定perl模組路徑
--with-perl=PATH 		# 設定perl庫檔案路徑
--http-log-path=PATH 	# 設定access log檔案路徑
--without-http 			# 不使用HTTP server功能
--without-mail_pop3_module 		# 不允許ngx_..模組
--without-mail_imap_module 		# 不允許ngx_..模組
--without-mail_smtp_module 		# 不允許ngx_..模組
--with-google_perftools_module 		# 允許ngx_..模組(除錯用)
--with-cpp_test_module 	# 允許ngx_..模組
--add-module=PATH 		# 允許使用外部模組,以及路徑
--with-cc=PATH 			# 設定C編譯器路徑
--with-cpp=PATH 		# 設定C預處理路徑
--with-ld-opt=OPTIONS 	# 設定連線檔案引數
--with-cpu-opt=CPU 		# 為指定CPU優化,可選引數有:
          pentium, pentiumpro, pentium3, pentium4,
          athlon, opteron, sparc32, sparc64, ppc64
--without-pcre 			# 不使用pcre庫檔案
--with-pcre=DIR 		# 設定PCRE庫路徑
--with-pcre-opt=OPTIONS # 設定PCRE執行引數
--with-md5=DIR 			# 設定md5庫檔案路徑
--with-md5-opt=OPTIONS 	# 設定md5執行引數
--with-md5-asm 			# 使用md5原始檔編譯
--with-sha1=DIR 		# 設定sha1庫檔案路徑
--with-sha1-opt=OPTIONS # 設定sha1執行引數
--with-sha1-asm 		# 使用sha1原始檔編譯
--with-zlib=DIR 		# 設定zlib庫檔案路徑
--with-zlib-opt=OPTIONS # 設定zlib執行引數
--with-zlib-asm=CPU 	# 使zlib對特定的CPU進行優化,可選引數:
          pentium, pentiumpro
--with-openssl=DIR 		# 設定OpenSSL庫檔案路徑
--with-openssl-opt=OPTIONS 	# 設定OpenSSL執行引數
--with-debug 			# 允許除錯日誌
--builddir=DIR 			# 設定程式編譯目錄
--with-rtsig_module 	# 允許rtsig模組
--with-select_module 	# 允許select模組(一種輪詢模式,不推薦用在高載環境)
--without-select_module # 不使用select模組
--with-poll_module 		# 允許poll模組(一種輪詢模式,不推薦用在高載環境)
--without-poll_module 	# 不使用poll模組

nginx編譯時不是功能加的越多越好,應該儘可能少編譯模組,不用的最好不要加入。
編譯好的nginx可通過 /usr/local/nginx/sbin/nginx -V 檢視編譯時的引數(具體路徑更具實際情況而定)

現將下列引數加入 #資料夾要自己建立

./configure 
--with-http_ssl_module \
--with-http_stub_status_module \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx/nginx.pid \
--with-openssl=/usr/local/src/openssl/openssl-1.0.1s/ \
--with-zlib=/usr/local/src/zlib/zlib-1.2.8/ \
--with-pcre=/usr/local/src/pcre/pcre-8.38/ \
--add-module=/usr/local/src/nginx/nginx-sticky-module-up/ \

( 自己備用 )

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:上邊將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄

# mkdir /var/temp/nginx -p

隨後執行 (以下所有的啟動目錄都是在上面 --sbin-path 中配置的存放路徑)

make && make install

啟動 nginx ,進入 /usr/local/nginx/nginx 目錄 執行 並檢視是否啟動

./nginx
ps -ef | grep nginx

通過web介面檢視時Nginx需要開啟status模組,加入
–with-http_stub_status_module
然後配置Nginx.conf,在server點裡面加入如下內容

location /status {
	stub_status on;
	access_log /usr/local/nginx/logs/status.log;
	auth_basic "NginxStatus"; 
}

配置完後重新啟動Nginx後我們可以通過瀏覽器訪問http://localhost/status 檢視
啟動成功

這裡寫圖片描述

判斷配置檔案是否正確,並重啟nginx

cd  /usr/local/nginx/nginx
./nginx -t
./nginx -s reload

知識小插曲
nginx建立虛擬主機,通過一臺主機繫結多個IP地址

1、將/etc/sysconfig/network-scripts/ifcfg-eth0檔案複製一份,命名為ifcfg-eth0:1
修改其中內容:
DEVICE=eth0:1
IPADDR=192.168.25.103
其他項不用修改
2、重啟系統
配置檔案下,http是主請求,server是虛擬主機,一個server表示一臺
通過增加server 並更改其下的 server_name xxx.xxx.xxx.xxx; 可以繫結到不同的IP伺服器
end

如果啟動成功,開始下一步 啟用 sticky 模組

在上面,我們已經通過 --add-module 為nginx安裝了 sticky 模組,在 upstream塊中新增 sticky; 即可啟用Session Sticky模組。

在 nginx.conf 中 http 下新增

upstream leo_test{
  sticky;
  server 127.0.0.1:80;
}

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback];

name: 可以為任何的string字元,預設是route
domain:哪些域名下可以使用這個cookie
path:哪些路徑對啟用sticky,例如path/test,那麼只有test這個目錄才會使用sticky做負載均衡
expires:cookie過期時間,預設瀏覽器關閉就過期,也就是會話方式。
no_fallbackup:如果設定了這個,cookie對應的伺服器宕機了,那麼將會返回502(bad gateway 或者 proxy error),建議不啟用

並在 local 下配置

location / {
	proxy_pass http://leo_test;
    include proxy.conf;
    add_header Cache-Control no-store;
}

再次檢驗是否正常,並重啟nginx

cd  /usr/local/nginx/nginx
./nginx -t
./nginx -s reload

關閉

查詢nginx主程序號

ps -ef | grep nginx

從容停止 kill -QUIT 主程序號

快速停止 kill -TERM 主程序號

強制停止 kill -9 nginx

若nginx.conf配置了pid檔案路徑,如果沒有,則在logs目錄下

kill -訊號型別 ‘/usr/local/nginx/logs/nginx.pid’

升級

1、先用新程式替換舊程式檔案

2、kill -USR2 舊版程式的主程序號或者程序檔名

此時舊的nginx主程序會把自己的程序檔案改名為.oldbin,然後執行新版nginx,此時新舊版本同時執行

3、kill -WINCH 舊版本主程序號

4、不過載配置啟動新/舊工作程序

kill -HUP 舊/新版本主程序號

從容關閉舊/新程序

kill -QUIT 舊/新程序號

快速關閉舊/新程序

kill -TERM 舊/新程序號

附:
如果報 “500 Internal Server Error”可能是設定的空間不夠

這裡寫圖片描述

最快方式–先檢查

$ ulimit -n 

如果過於小更改為 51200

ulimit -SHn 51200 

其他方式 ↓↓↓↓↓

nginx.conf 檔案改為

worker_processes 2
worker_cpu_affinity 0001 0010;

假如是8 cpu 分配如下:

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000
00100000 01000000 10000000

linux 2.6核心下開啟檔案開啟數為65535,worker_rlimit_nofile就相應
應該填寫65535

worker_rlimit_nofile 102400;
worker_connections 65535;

開啟gzip

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css
application/xml;
gzip_vary on;

快取靜態檔案:

location ~* ^.+\.(swf|gif|png|jpg|js|css)$ {
	root /usr/local/ku6/ktv/show.ku6.com/;
	expires 1m;
}

優化Linux核心引數

vi /etc/sysctl.conf

Add

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535

配置keepalived 實現 nginx 高可用(展示還未了解清楚,待定)
原理:nginx至少要配置兩臺伺服器,一主一備,備用伺服器時刻監聽主體資訊,如果主體資訊一掛,立馬啟用備用。