1.1.1 本文檔爲nginx安裝及配置文檔,主要實現http反向代理功能;作用是將nginx作爲前端服務器,通過訪問規則代理轉發至後端對應的tomcat服務器
部署環境:
系統版本:Linux version 2.6.32-431.el6.x86_64(`cat /proc/version`)
安裝及配置:
1安裝nginx依賴環境
安裝pcre庫
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
#tar xvzf pcre-8.21.tar.gz
#cd pcre-8.21
#./configure && make && make install
安裝zlib庫
#wget http://zlib.net/zlib-1.2.7.tar.gz
#tar zxvf zlib-1.2.7.tar.gz
#cd zlib-1.2.7.tar.gz
#./configure
#make
#make install
安裝nginx
#wget http://nginx.org/download/nginx-1.6.2.tar.gz
#tar zxvf nginx-1.6.2.tar.gz
#cd nginx-1.6.2
# ./configure--sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7
#make
#make install
1.2 關閉防火牆
#/etc/init.d/iptables stop
1.3 啓動nginx和停止
/usr/local/nginx/nginx
Ps –ef |grep nginx
停止
Pkill nginx kill –QUIT n //n表示nginx的進程號
1.4 定製配置文件
Nginx對外的端口是80,所以確保80端口不被其他程序佔用(特別是tomcat)
修改配置文件,
#vim /usr/local/nginx/nginx.conf
upstream tgmas {
server111.11.84.32:8080 max_fails=3 fail_timeout=3s;
server111.11.84.33:8080 max_fails=3 fail_timeout=3s;
ip_hash;
}
在server{}範圍添加如下代碼:
location ~* ^.+\.(js|css)$ {
access_log off;
expires 1d;
break;
}
location ~*^.+\.(jpg|jpeg|gif|png|bmp)$ {
access_log off;
expires 3d;
break;
}
location ^~/wygj/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tgmas;
}
注:(1)location ~*^.+\.(js|css)$ 所有以.js或.css結尾的訪問操作;
(2)location ~* ^.+\.(jpg|jpeg|gif|png|bmp)$ 所有訪問圖片的操作;
(3)^~ /wygj/ 所有以/wygj/結尾的訪問操作;
(4)access_log off 訪問時不開啓日誌;
(5)expires 過期時間;
(6)proxy_pass 反向代理路徑,即指向某個業務tomcat服務器;
(7) upstream tgmas代理服務器羣組
(8) max_fails=3 fail_timeout=3s訪問單臺服務器最大錯誤數及訪問超時時間;
(9) ip_hash負載策略,session同步
1.4.1 訪問
以上述參數爲例,以下訪問地址就會跳轉至http://127.0.0.1:8080
http://127.0.0.1/wygj
平滑升級:
1用新的可執行程序替換舊的可執行程序,對於編譯安裝的nginx可以將新的可執行文件安裝到舊的可執行文件,需要先備份舊版本的可執行文件,再進行編譯安裝新的nginx
2kill –USR2 舊版本的主進程號
3這個時候舊版本的nginx的主進程爲.pid.oldbin ,然後開啓新版本的nginx可執行文件,依次啓動主進程和新的工作進程
4現在新,舊的nginx會同時運行(ps –ef|grepnginx),共同處理請求。現在需要停止舊版本的nginx,必須發送WINCH信號給主進程,然後它的工作將開始從容關閉
Kill –WINCH 舊版本的主進程號
5 一段時間後,舊的工作進程(workproess)處理了所有已連接的請求後退出,由新的工作進程來處理請求了
2 詳細操作如下:
1 查看當前nginx版本和編譯路徑
#/usr/local/nginx/nginx –v
nginx/1.2.9
#/usr/local/nginx/nginx –V
nginx version: nginx/1.2.9
built by gcc 4.4.7 20120313 (Red Hat4.4.7-4) (GCC)
configure arguments:--sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf--pid-path=/usr/local/nginx/nginx.pid --with-pcre=/root/pcre-8.36--with-zlib=/root/zlib-1.2.8
2 使用新的可執行程序替換舊的可執行程序,對於編譯安裝的nginx,可以將新版本編譯安裝到舊版本的安裝路徑,替換之前,最好先備份一下舊的可執行文件
#cp /usr/local/nginx/nginx ~/
#tar xvzf nginx-1.6.2.tar.gz
#cd nginx0-1.6.2
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module
# make && make install
3 查看一下當前的主進程號是多少,然後執行 kill –USR2 21421(舊版主進程號)
Ps –ef|grepnginx
1. root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
2. nobody 24021 21421 0 20:49 ? 00:00:00 nginx: worker process
3. root 28765 24076 0 22:04 pts/1 00:00:00 grep nginx
#kill –USR2 21421
4 這時候舊版的nginx的主進程將重命名它的.pid 文件爲.oldbin(例如:/usr/local/nginx/nginx.pid.oldbin),然後執行新的版本nginx可執行文件
# /usr/local/nginx/nginx
# Ps–ef |grep nginx
1. root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
2. nobody 24021 21421 0 20:49 ? 00:00:00 nginx: worker process
3. root 28768 21421 0 22:04 ? 00:00:00 nginx: master process nginx
4. nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
5. root 28786 24076 0 22:11 pts/1 00:00:00 grep nginx
5 此時,新,舊版本的nginx會同時運行,共同處理輸入的請求,要逐步停止舊版本的nginx,必須發送WINCH信號給舊的主進程,然後他的工作進程將開始從容關閉
# Kill –WINCH `cat /usr/local/nginx/nginx.pid.oldbin`
6 一段時間後,舊的工作進程(work process)處理了所有已連接的請求後退出,僅由新的工作進程來處理
1. # ps -ef | grep nginx
2. root 21421 1 0 20:37 ? 00:00:00 nginx: master process nginx
3. root 28768 21421 0 22:04 ? 00:00:00 nginx: master process nginx
4. nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
root 28799 24076 0 22:15 pts/1 00:00:00 grep nginx
7 發送QUIT信號給舊的主進程,使其退出而只留下新的nginx服務器運行
#Kill –QUIT ·cat /usr/local/nginx/nginx.pid.oldbin·
# ps –ef |grep nginx
1. root 28768 1 0 22:04 ? 00:00:00 nginx: master process nginx
2. nobody 28769 28768 0 22:04 ? 00:00:00 nginx: worker process
3. root 28808 24076 0 22:20 pts/1 00:00:00 grep nginx