Nginx的平滑升级、SSL的站点配置
Nginx的平滑升级
1.下载新版本并安装
本次环境为:centos6.10。原nginx版本为1.15.3,准备变更的版本为1.14.2。
[root@puppetmaster sbin]# ./nginx -V
nginx version: nginx/1.15.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
configure arguments: --add-module=/usr/local/src/ngx_http_hello_nginx_module --prefix=/usr/local/nginx
1.1备份旧的可执行文件
[root@puppetmaster nginx-1.14.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
1.2下载并安装nginx
从nginx.net下载,使用 tar zxvf nginx-1.14.2.tar.gz解压缩,依次./configure,make,make install
[root@puppetmaster nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
2.开启并行运行并观察
make install之后,nginx可执行文件已经变成了新版本。
[root@puppetmaster nginx-1.14.2]# ll /usr/local/nginx/sbin/
total 10952
-rwxrwxrwx 1 root root 25475 Dec 27 09:00 etiantian.log
-rwxr-xr-x 1 root root 3679011 Apr 11 14:42 nginx
-rwxr-xr-x 1 root root 3748254 Apr 10 18:07 nginx.bak
-rwxr-xr-x 1 root root 3748254 Apr 10 18:05 nginx.old
同时,观察一下nginx的运行情况,nginx的pid和pid文件(nginx正常运行,pid为7665):
[root@puppetmaster nginx-1.14.2]# curl localhost
hahahaha
[root@puppetmaster nginx-1.14.2]# cat /usr/local/nginx/logs/nginx.pid
7665
[root@puppetmaster nginx-1.14.2]# ll /usr/local/nginx/logs/
total 948
-rwxrwxrwx 1 root root 795 Dec 12 18:12 20181212access.log
-rwxrwxrwx 1 root root 661701 Apr 11 14:42 access.log
-rwxrwxrwx 1 root root 53800 Dec 13 11:14 access_www.log
-rwxrwxrwx 1 root root 213238 Apr 11 13:40 error.log
-rw-r--r-- 1 root root 5 Apr 11 13:42 nginx.pid
-rwxrwxrwx 1 root root 11646 Dec 24 11:51 www_access.log
发送以下指令,原pid文件被命名为nginx.pid.oldbin:
[root@puppetmaster nginx-1.14.2]# kill -usr2 7665
[root@puppetmaster nginx-1.14.2]# curl localhost
hahahaha
[root@puppetmaster nginx-1.14.2]# ll /usr/local/nginx/logs/
total 952
-rwxrwxrwx 1 root root 795 Dec 12 18:12 20181212access.log
-rwxrwxrwx 1 root root 662638 Apr 11 14:43 access.log
-rwxrwxrwx 1 root root 53800 Dec 13 11:14 access_www.log
-rwxrwxrwx 1 root root 213310 Apr 11 14:43 error.log
-rw-r--r-- 1 root root 6 Apr 11 14:43 nginx.pid
-rw-r--r-- 1 root root 5 Apr 11 13:42 nginx.pid.oldbin
-rwxrwxrwx 1 root root 11646 Dec 24 11:51 www_access.log
[root@puppetmaster nginx-1.14.2]# cat /usr/local/nginx/logs/nginx.pid
10987
[root@puppetmaster nginx-1.14.2]# cat /usr/local/nginx/logs/nginx.pid.oldbin
7665
此时,新旧2个nginx进程同时在运行:
[root@puppetmaster sbin]# netstat -tunlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7665/nginx
udp 0 0 fe80::20c:29ff:fe4d:c99d:123 :::*
发送以下指令,逐步关闭旧nginx进程:
[root@puppetmaster sbin]# kill -winch 7665
此时依然是2个进程同时运行,共同处理输入的请求。
3.完成切换
一段时间后,旧的工作进程处理了所有已连接请求后退出,仅由新的工作进程来处理请求了。
下图为新旧进程同时运行,新进程在处理连接:
[root@puppetmaster sbin]# ps aux|grep nginx
root 7665 0.0 0.0 22232 1464 ? Ss 13:42 0:00 nginx: master process ./nginx
root 10987 0.0 0.0 22092 1620 ? S 14:43 0:00 nginx: master process ./nginx
nobody 10988 0.0 0.0 22456 1584 ? S 14:43 0:00 nginx: worker process
root 11253 0.0 0.0 6448 680 pts/2 S+ 15:01 0:00 grep nginx
这时,需要决定使用新版本,还是恢复到旧版本。
如果恢复到旧版本:
kill -HUP 旧的主进程号:nginx将在不重载配置文件的情况下启动它的工作进程;
kill -QUIT 新的主进程号:从容关闭其工作进程;
kill -TERM 新的主进程号:强制退出;
新进程退出后,就主进程会移除.oldbin前缀,恢复它的.pid文件。
如果使用新版本,退出旧进程即可:
[root@puppetmaster sbin]# kill -quit 7665
最后,热升级完成。虽然实际上是降级:
[root@puppetmaster sbin]# ps aux|grep nginx
root 10987 0.0 0.0 22092 1620 ? S 14:43 0:00 nginx: master process ./nginx
nobody 10988 0.0 0.0 22456 1584 ? S 14:43 0:00 nginx: worker process
root 11263 0.0 0.0 6448 680 pts/2 S+ 15:02 0:00 grep nginx
[root@puppetmaster sbin]# netstat -tunlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10987/nginx
udp 0 0 fe80::20c:29ff:fe4d:c99d:123 :::* 2193/ntpd
[root@puppetmaster sbin]# curl localhost
hahahaha
[root@puppetmaster sbin]# ls
etiantian.log nginx nginx.bak nginx.old
[root@puppetmaster sbin]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
configure arguments: --prefix=/usr/local/nginx
SSL的站点配置
1.准备
确保nginx编译时加上了–with-http_ssl_module模块。假如nginx的启动文件在/usr/local/nginx/sbin目录下,使用如下命令查看:
[root@puppetmaster sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.2h 3 May 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-openssl=/usr/local/src/openssl-1.0.2h
如果没有ssl模块,重新编译一下,注意–with-openssl应该是openssl的源码文件夹位置,不是openssl可执行文件的位置。
2.生成ssl证书
2.1 生成server.key文件:
openssl genrsa -des3 -out server.key 2048
除去输入密码的步骤:
openssl rsa -in server.key -out server.key
2.2 创建服务器证书的申请文件:
openssl req -new -key server.key -out server.csr
其中Country Name填CN,Common Name填主机名也可以不填,如果不填浏览器会认为不安全.(例如你以后的url为https://abcd/xxxx…这里就可以填abcd),其他的都可以不填。
2.3 创建CA证书:
openssl req -new -x509 -key server.key -out ca.crt -days 3650
2.4 创建自当前日期起有效期为期十年的服务器证书server.crt:
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
2.5 ls你的文件夹,可以看到一共生成了5个文件:
ca.crt ca.srl server.crt server.csr server.key
其中,server.crt和server.key就是你的nginx需要的证书文件。
3.修改站点配置
站点配置如下:
server
{
listen 443;
server_name miaomiaomiao.com.cn;
ssl on;
ssl_certificate /home/zhangsx/openssl/server.crt;
ssl_certificate_key /home/zhangsx/openssl/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
root html;
index index.html index.php;
location ~ .*\.php$
{
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include fastcgi_params;
}
ssl_certificate和ssl_certificate_key的位置根据自己实际情况填写;记得ssl on一定要写,不然不生效;测试时记得配置/etc/hosts文件。
最后,./nginx -t检查系统配置后,./nginx -s reload重新载入配置文件。