一.私有CA
1.前言CA 证书颁发机构(CA, Certificate Authority)
基于https的协议工作的一中虚拟主机,要构建这样的网站需要mod_ssl模块的支持。且需要提供两个文件:证书文件和私钥文件,证书文件是标识这个网站服务器身份的,私钥文件主要用来实现在服务器端对数据进行加密,然后在网站中传输的。证书在生产生活中需要到对应的机构去申请,在实验环境中本应该搭建一台证书服务器。
2. 生成证书及秘钥文件
1)准备存放证书和秘钥的目录
mkdir -p /etc/nginx/ssl
2)生成私钥
使用openssl生成基于rsa数学算法长度为1024bit的秘钥,文件必须以key为结尾
[root@nginx ~]# openssl genrsa 1024 > /etc/nginx/ssl/server.key
Generating RSA private key, 1024 bit long modulus
...............................................................++++++
................................................................++++++
e is 65537 (0x10001)
- 使用秘钥文件生成证书-申请书
[root@nginx ~]# openssl req -new -key /etc/nginx/ssl/server.key > /etc/nginx/ssl/server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN ###国家名(两个字
State or Province Name (full name) []:BJ ###省会(两个字
Locality Name (eg, city) [Default City]:BJ ###城市
Organization Name (eg, company) [Default Company Ltd]::qf ###组织名 (千锋QF
Organizational Unit Name (eg, section) []:cloud ##组织单位名
Common Name (eg, your name or your server's hostname) []:nginx.linux.com ##服务器的名字或者你的名字
Email Address []:12345678@qq.com ###可选
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ###密码为空
An optional company name []: ####密码为空
Common Name (eg, your name or your server's hostname) []: ###公司名空
查看申请书
[root@nginx ~]# ls /etc/nginx/ssl/
server.csr (证书申请) server.key (私钥)
4)同意申请,生成证书
[root@nginx ~]# openssl req -x509 -days 365 -key /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr > /etc/nginx/ssl/server.crt
注释
-x509:证书的格式,固定的
days:证书的有效期,生产生活中时间不同,价格不同
key:指定秘钥文件
in:指定证书申请文件
查看证书
ll /etc/nginx/ssl/
3、私有CA的https部署实战
1)创建目录
2)编辑nginx.conf文件
[root@nginx ~]# vim /etc/nginx/conf.d/bj.conf
server {
listen 443 ssl;
server_name www.bj.com;
ssl_certificate /etc/nginx/ssl/server.crt; ##路径自定义
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
root /bj;
index index.html index.htm;
}
}
[root@nginx ~]# nginx -t
[root@nginx ~]# nginx -s reload
[root@nginx ~]# ss -antp | grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=11700,fd=6),("nginx",pid=11699,fd=6),("nginx",pid=8347,fd=6))
LISTEN 0 128 *:443 *:* users:(("nginx",pid=11700,fd=20),("nginx",pid=11699,fd=20),("nginx",pid=8347,fd=20))
3)测试访问 https://www.bj.com
二.公网CA
[root@xiaochen ~]# ll /etc/nginx/214194377980730.*
-rw-r--r-- 1 root root 1679 May 11 14:41 /etc/nginx/214194377980730.key
-rw-r--r-- 1 root root 3916 May 11 14:41 /etc/nginx/214194377980730.pem
[root@xiaochen ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.xuleicloud.top ;
return 301 https://www.xuleicloud.top$request_uri;
# rewrite .* https://www.xuleicloud.top$request_uri permanent;
}
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/nginx/214194377980730.pem;
ssl_certificate_key /etc/nginx/214194377980730.key;
location / {
root /usr/share/nginx/html;
index index.html index.php;
}
}
[root@xiaochen ~]# systemctl restart nginx
三.Nginx的平滑升级
1.原理:
当需要将正在运行中的nginx升级,添加/删除服务模块时,可以在不中断服务的情况下,使用新版本,重编译的Nginx可执行程序替换旧版本的可执行程序,步骤如下:
• 使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx,可以将新版本编译安装到旧版本的nginx安装路径中.替换之前,最好备份一下旧的可执行程序
• 发送以下指令: Kill –USR2 旧版本的nginx主进程号
• 旧版本的主进程将重命名它的pid文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程.
• 此时,新,旧版本的nginx实例会同时运行,共同处理输入的请求.要逐步停止旧版本的nginx实例,你必须发送WINCH信号给旧的主进程,然后,它的工作进程就将开始从容关闭:kill –WINCH 旧版本的Nginx主进程号
• 一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了.
• 这时候,我们可以决定是使用新版本,还是恢复到旧的版本;
Kill –HUP 旧的主进程号:Nginx将在不重载配置文件的情况下启动它的工作进程;
Kill –QUIT 新的主进程号:从容关闭其他工作进程(woker process);
Kill –TERM 新的主进程号:强制退出;
Kill 新的主进程号或旧的主进程号:如果因为某些原因新的工作进程不能退出,则向其发送kill信号.
新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为他的.pid文件,这样,一切就都恢复到升级之前了,如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留下新的服务器运行:
2.平滑升级1.12版本到1.14版本
1)编译安装新版本的nginx,指定安装目录为新目录
[root@server nginx]# tar xf nginx-1.14.2.tar.gz -C /usr/local/src/
[root@server nginx]# cd /usr/local/src/nginx-1.14.2/
[root@server nginx-1.14.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx14 --with-http_stub_status_module --with-http_ssl_module && make && make install
2)查看就的nginx的主进程号和工作进程号
[root@server ~]# ps aux |grep ngin[x]
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 75083 0.0 0.1 21060 1632 ? S 12:17 0:00 nginx: worker process
3)替换旧的执行程序
[root@server ~]# mv /usr/local/nginx/sbin/nginx{,.bak}
[root@server ~]# cp /usr/local/nginx14/sbin/nginx /usr/local/nginx/sbin/nginx
[root@server ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2
4) 给主进程发送USR2信号
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid
68595
[root@server ~]# kill -USR2 68595
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid.oldbin
68595
旧版本的主进程将重命名它的pid文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程.
5)给进程发送WINCH信号
[root@server ~]# kill -WINCH 68595
[root@server ~]# ps aux |grep ngin[x]
root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了.
3.回退到以前版本
原理
这时因为旧的服务器还尚未关闭它监听的套接字,所以通过下面的几步还可以恢复旧版本:
• 发送 HUP 信号给旧的主进程 - 它将在不重载配置文件的情况下启动它的工作进程。
• 发送 QUIT 信号给新的主进程,要求其从容关闭其工作进程
• 发送 TERM 信号给新的主进程,迫使其退出
• 如果因为某些原因新的工作进程不能退出,则直接将其杀死 KILL 信号
第一步
[root@server ~]# kill -HUP 68595
[root@server ~]# ps aux |grep ngin[x]
root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 80008 0.0 0.1 21060 1388 ? S 13:50 0:00 nginx: worker process
第二步
[root@server ~]# kill -QUIT 58943
[root@server ~]# ps aux |grep ngin[x]
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 80008 0.0 0.1 21060 1388 ? S 13:50 0:00 nginx: worker process
[root@server ~]# cat /usr/local/nginx/logs/nginx.pid
68595
4.总结
新的主进程退出后,旧的主进程会自动移除 .oldbin 后缀,恢复为.pid的后缀名,如此:一切就都恢复到升级之前了。如果尝试升级成功,而你也希望保留新的服务器时,发送 QUIT 信号给旧的主进程使其退出而只留下新的服务器运行。