Zakariyya / blog

https://zakariyya.github.io/blog/
6 stars 1 forks source link

nginx服务器安装ssl证书教程 #172

Open Zakariyya opened 1 month ago

Zakariyya commented 1 month ago

查看nginx

nginx -V

nginx version: nginx/1.22.1
built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
 ...

可以看到该 nginx版本为 1.22.1,加密软件:OpenSSL 3.0.7 1

上传证书

在证书管理页面下载适用于 nginx服务器 的证书。

将下载的证书文件上传到服务器中Nginx系统目录下的 cert 目录(例如/etc/nginx/conf.d/ssl)

cd /etc/nginx/conf.d 
mkdir ssl

上传.zip包版本的证书,里面有pem和key文件

unzip 证书.zip  #解压证书

mv xxx.pem yourDomain.pem  # 将.pem证书文件文件改名为sslconfigure.pem (建议改为你自己的域名:yourDomain.com)
mv xxx.pem yourDomain.key  # 将.key私钥文件改名为sslconfigure.key (建议改为你自己的域名:yourDomain.com)

在服务器上部署证书

vim /etc/nginx/nginx.conf  ## 查询nginx域名配置文件的路径,找到以下内容,显示为/etc/nginx/conf.d/*.conf
# include /etc/nginx/conf.d/*.conf

cd /etc/nginx/conf.d

vim 你域名.com.conf   # 如域名叫:yourDomain.com

内容如下:
------------------
server {
  listen 80;
  server_name www.yourDomain.com yourDomain.com;
  gzip on;
  gzip_min_length 100;
  gzip_types text/plain text/css application/xml application/javascript;
  gzip_vary on;

  location / {
  }
}

# 开启 443 协议 Settings for a TLS enabled server.

server {
  listen       443 ssl http2;
  listen       [::]:443 ssl http2;
  server_name  yourDomain.com yourDomain.com; ##改为你的网站域名
  root         html;

  ssl_certificate "/etc/nginx/conf.d/ssl/yourDomain.com.pem";  ##这里是你ssl的文件路径,别填错
  ssl_certificate_key "/etc/nginx/conf.d/ssl/yourDomain.com.key"; ##这里是你ssl的文件路径,别填错
  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout  10m;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1.1 TLSv1.2;  ## 根据需要修改加密协议,由于 **TLS v1** 存在不安全的算法,如果需要较高的安全性,建议删除 **TLS v1** ,demo中已经删除,不用修改

  # Load configuration files for the default server block.
  include /etc/nginx/default.d/*.conf; ##这个是你nginx要读取的配置文件

  location / {
  }

}

------------------

nginx 校验语法

nginx -t ## 检查nginx配置语法

如果返回: 提示如下:no such file 或者 test failed

nginx:[emerg]B10_new_file(/cert/ss1conrigure.pem) failed (SSL:error:02001002:system 1ibrary:fopen:No such file or directory:fopen('/cert/sslconfigure.pem','r') error:2006D080:B10 routines:B10_new_file:no such file) nginx:configuration file /etc/nginx/nginx.conf test failed

上述提示是找不到证书文件,说明文件路径可能配置不正确

直到校验通过: nginx:theconfiguration file/etc/nginx/nginx.confsumtax is ok nginx:configurationfile/etc/nginx/nginx.conftest is successful

重新加载 nginx

nginx -s reload

检查nginx和证书部署情况

netstat -nplt | grep 80  ##查询80端口的状态

tcp   0  0 0.0.0:80   0.0.0.0:*  listem   6515/nginx: master
tcp6  0  0 :::80      :::*       listem   6515/nginx: master

此时80端口已开启监听
通过浏览器http协议访问域名时提示:链接不安全, 说明没有开启443 https协议,回到上一步重新修改conf

netstat -nplt | grep 443  ##查询443端口的状态

# tcp   0  0 0.0.0:443   0.0.0.0:*  listem   6515/nginx: master

firewall-cmd --state ##查询防火墙状态
firewall-cmd --list-ports ##查询服务器的防火墙是否放行了443端口
firewall-cmd --zone=public --add-port=443 --permanent  ##防火墙添加443端口
firewall-cmd --reload  ##重启防火墙
firewall-cmd --list-ports ##重新查看是否放行了443端口

验证部署

echo | openssl s_client -connect 127.0.0.1:443 -servername yourDomain.com 2>/dev/null

结果显示:
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session: 
    Protocol  : TLSv1.2 ##-------------------------》》》》 出现这个就对了
    Cipher    : ECDHE-RSA-******-***-******
    Session-ID: C90C03**********************************************DB463E
    Session-ID-ctx:
    Master-Key: C0A*********************************************************************************00
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 600 (seconds)
    TLS session ticket:
    0000 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0010 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0020 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0030 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0040 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0050 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0060 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0070 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0080 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    0090 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    00a0 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    00b0 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************
    00c0 - ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **   ****************

    Start Time: 1717324694
    Timeout   : 7200 (sec)
    Verify return code: 0 (ok)
    Extended master secret: yes

分别在浏览器和服务端上校验

浏览器上:https://yourDomain.com

终端:curl -I https://yourDomain.com

配置http 强制跳转 https

cd /etc/nginx/conf.d

vim yourDomain.conf

------------------
server {
  listen 80;
  rewrite ^(.*) https://$server_name$1 permanent;  ## 添加这个就能强制跳转https
  server_name www.yourDomain.com yourDomain.com;
  gzip on;
  gzip_min_length 100;
  gzip_types text/plain text/css application/xml application/javascript;
  gzip_vary on;

  location / {
  }
}

------------------

nginx -t
nginx -s reload

在服务端上执行,验证http协议的响应请求:

curl -I http://yourDomain.com