1.1.1 本文档为nginx安装及配置文档,主要实现http反向代理功能;作用是将nginx作为前端服务器,通过访问规则代理转发至后端对应的tomcat服务器
部署环境:
系统版本:Linux version 2.6.32-431.el6.x86_64(`cat /proc/version`)
安装及配置:
1安装nginx依赖环境
安装pcre库
#tar xvzf pcre-8.21.tar.gz
#cd pcre-8.21
#./configure && make && make install
安装zlib库
#tar zxvf zlib-1.2.7.tar.gz #cd zlib-1.2.7.tar.gz #./configure #make #make install
安装nginx
#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/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
|