ssl 模块支持

  1. ./sbin/nginx -s reload
  2. nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module

查看Nginx安装的模块

  1. /usr/local/nginx/sbin/nginx -V
  2. nginx version: nginx/1.14.2
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  4. configure arguments: --prefix=/usr/local/nginx

在configure arguments:后面显示的原有的configure参数,不支持ssl模块,需要增加ssl 模块

  1. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

查看当前Nginx是否在运行

  1. # ps -ef|grep nginx
  2. root 1270 1 0 410 ? 00:00:00 nginx: master process sbin/nginx
  3. nobody 3762 1270 0 10:56 ? 00:00:00 nginx: worker process
  4. root 4104 4082 0 15:29 pts/1 00:00:00 grep --color=auto nginx

备份原有已安装好的nginx二进制文件

  1. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

重新编译新的Nginx

  1. #cd ~/nginx-1.14.2
  2. #./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  3. # make
  4. # cp ./objs/nginx /usr/local/nginx/sbin/ -rf
  5. cp:是否覆盖"/usr/local/nginx/sbin/nginx" y

热部署

  1. # kill -USR2 1270
  2. [root@aliyun sbin]# ps -ef|grep nginx
  3. root 1270 1 0 410 ? 00:00:00 nginx: master process sbin/nginx
  4. nobody 3762 1270 0 10:56 ? 00:00:00 nginx: worker process
  5. root 6676 1270 0 15:43 ? 00:00:00 nginx: master process sbin/nginx
  6. nobody 6677 6676 0 15:43 ? 00:00:00 nginx: worker process
  7. root 6679 4082 0 15:44 pts/1 00:00:00 grep --color=auto nginx

关闭原先的work 保留master 实现回滚

  1. kill -WINCH 1270

本地制作证书

  1. 1. 生成服务器端的私钥 (key 文件)
  2. $openssl genrsa -out server.key 1024
  3. 2. 生成服务器端证书签名请求文件 (csr 文件);
  4. $ openssl req -new -key server.key -out server.csr
  5. ...
  6. Country Name:CN------------ 证书持有者所在国家
  7. State or Province Name:BJ-- 证书持有者所在州或省份(可省略不填)
  8. Locality Name:BJ----------- 证书持有者所在城市(可省略不填)
  9. Organization Name:SC------- 证书持有者所属组织或公司
  10. Organizational Unit Name:.- 证书持有者所属部门(可省略不填)
  11. Common Name :ceshi.com----- 域名
  12. Email Address:------------- 邮箱(可省略不填)
  13. A challenge password:------ 直接回车
  14. An optional company name:-- 直接回车
  15. 3. 生成证书文件 (crt 文件)
  16. $ openssl x509 -req -days 1000 -in server.csr -signkey server.key -out server.crt

申请ssl证书

阿里云免费ssl证书
image.png
腾讯云免费ssl证书
image.png

  1. server {
  2. listen 443 ssl;
  3. server_name baxiang.club www.baxiang.club;
  4. ssl_certificate 1_www.baxiang.club_bundle.crt;
  5. ssl_certificate_key 2_www.baxiang.club.key;
  6. ssl_session_cache shared:SSL:1m;
  7. ssl_session_timeout 5m;
  8. ssl_ciphers HIGH:!aNULL:!MD5;
  9. ssl_prefer_server_ciphers on;
  10. location / {
  11. root html;
  12. index index.html index.htm;
  13. }
  14. }

修改配置完成后,重启 nginx 服务

  1. nginx -s reload      //使配置生效

访问 https://baxiang.club

image.png

配置 http 强制跳转 https

在 nginx 配置文件中的 server 区域添加如下内容

  1. if ($scheme = 'http') {
  2. rewrite ^(.*)$ https://$host$uri;
  3. }