Nginx SSL/TLS 配置指南
前言
1. 准备工作:安装 Nginx
1.1 安装 EPEL 仓库
1.2 安装 Nginx
1.3 启动 Nginx 并设置开机启动
1.4 验证 Nginx 是否正常运行
2. 获取 SSL/TLS 证书
2.1 使用 Let's Encrypt 免费证书(推荐)
2.1.1 安装 Certbot
2.1.2 获取 SSL 证书
2.1.3 配置自动续期
2.2 创建自签名证书(仅用于测试)
2.2.1 生成自签名证书
3. 配置 Nginx 启用 SSL/TLS
3.1 编辑 Nginx 配置文件
3.2 配置 SSL 证书路径
3.3 配置 HTTP 到 HTTPS 重定向
3.4 配置 HTTP/2(可选)
4. 强化 SSL/TLS 安全性
4.1 禁用弱加密协议
4.2 启用 HTTP Strict Transport Security (HSTS)
4.3 启用更强的 Diffie-Hellman 参数
4.4 配置加密套件
5. 验证配置
5.1 检查 Nginx 配置
5.2 重启 Nginx
5.3 测试 HTTPS 配置
6. 定期更新证书
7.问题排查
1️⃣ 错误:SSL_CTX_set_cipher_list failed
2️⃣ 错误:ssl parameter requires ngx_http_ssl_module
3️⃣ 错误:The certificate is not trusted because it is self-signed
4️⃣ 错误:The page isn’t redirecting properly
5️⃣ 错误:The certificate is not valid for the name 192.168.10.50
6️⃣ 错误:/bin/sh: ./config: No such file or directory
7️⃣ 错误:gzip: stdin: not in gzip format
总结
前言
在当今的互联网环境中,确保数据传输的安全性至关重要。SSL/TLS 协议通过加密通信内容,有效防止数据被窃取或篡改。无论是个人网站还是企业级应用,启用 HTTPS 已成为基本的安全实践。本指南详细介绍了如何在 Nginx 服务器上配置 SSL/TLS 证书,涵盖从证书获取到安全强化的全流程。通过遵循这些步骤,可以轻松实现网站的安全升级,提升用户信任度并满足现代浏览器的安全要求。
1. 准备工作:安装 Nginx
在开始启用 SSL/TLS 之前,首先确保 Nginx 已经正确安装并运行在你的服务器上。下面是安装和启动 Nginx 的详细步骤。
1.1 安装 EPEL 仓库
首先,你需要安装 EPEL 仓库,因为 Nginx 包含在这个仓库中。可以通过以下命令完成安装:
sudo yum install epel-release -y
1.2 安装 Nginx
接着,你可以通过以下命令安装 Nginx:
sudo yum install nginx -y
1.3 启动 Nginx 并设置开机启动
安装完成后,启动 Nginx 并设置开机自动启动:
sudo systemctl start nginx sudo systemctl enable nginx
1.4 验证 Nginx 是否正常运行
为了确保 Nginx 正常运行,你可以访问 http://your_server_ip
来检查。如果无法访问,可以使用以下命令查看 Nginx 的状态:
sudo systemctl status nginx
这样,你就可以确认 Nginx 是否已经成功启动。
2. 获取 SSL/TLS 证书
SSL/TLS 证书是启用 HTTPS 的基础,它能保证网站与用户之间的通讯加密。在这里,我们提供了两种方式来获取证书:使用 Let's Encrypt 免费证书(推荐)和创建 自签名证书。
2.1 使用 Let's Encrypt 免费证书(推荐)
Let's Encrypt 提供免费的 SSL/TLS 证书,并且可以自动续期,非常适合生产环境使用。我们将通过 Certbot 工具来自动获取并配置证书。
2.1.1 安装 Certbot
首先,安装 Certbot 和 Nginx 插件,这些工具将帮助你自动化证书的获取和配置:
sudo yum install epel-release -y sudo yum install certbot python2-certbot-nginx -y
2.1.2 获取 SSL 证书
使用 Certbot 获取证书时,只需运行以下命令,Certbot 会自动为你配置 SSL 和 HTTPS:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
请确保将 yourdomain.com
替换为你的实际域名。Certbot 会自动修改 Nginx 配置并重新加载 Nginx 服务。
2.1.3 配置自动续期
Let's Encrypt 证书有效期为 90 天,因此你需要定期续期。为了自动化这个过程,可以设置一个每日检查并自动续期证书的任务:
sudo crontab -e
在打开的文件中添加以下行:
0 0 * * * /usr/bin/certbot renew --quiet
这样,Certbot 将会每天检查并自动续期证书,确保你的网站一直处于 HTTPS 保护之下。
2.2 创建自签名证书(仅用于测试)
如果你只是在开发或测试环境中使用 SSL,可以创建自签名证书。虽然自签名证书没有可信任的根证书,但它可以帮助你测试 HTTPS 配置。
2.2.1 生成自签名证书
首先,你需要生成一个密钥文件和证书签名请求(CSR):
sudo mkdir /etc/ssl/private sudo mkdir /etc/ssl/certs openssl genpkey -algorithm RSA -out /usr/local/nginx/ssl/private/nginx-selfsigned.key -pkeyopt rsa_keygen_bits:2048 openssl req -new -key /usr/local/nginx/ssl/private/nginx-selfsigned.key -out /usr/local/nginx/ssl/certs/nginx-selfsigned.csr
生成 CSR 时,系统会要求你填写一些信息,例如国家、组织等。
接下来,生成自签名证书:
openssl x509 -req -days 365 -in /usr/local/nginx/ssl/certs/nginx-selfsigned.csr -signkey /usr/local/nginx/ssl/private/nginx-selfsigned.key -out /usr/local/nginx/ssl/certs/nginx-selfsigned.crt
这样,你就完成了自签名证书的生成。
3. 配置 Nginx 启用 SSL/TLS
配置完证书后,我们需要配置 Nginx 使其支持 SSL/TLS。以下是配置步骤。
3.1 编辑 Nginx 配置文件
打开 Nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
3.2 配置 SSL 证书路径
在配置文件中,添加以下内容来启用 SSL 和指定证书路径:
server {listen 443 ssl;server_name yourdomain.com www.yourdomain.com; ssl_certificate /usr/local/nginx/ssl/certs/nginx-selfsigned.crt;ssl_certificate_key /usr/local/nginx/ssl/private/nginx-selfsigned.key;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;ssl_prefer_server_ciphers on; location / {root /usr/share/nginx/html;index index.html;} }
3.3 配置 HTTP 到 HTTPS 重定向
为了确保所有 HTTP 流量都被重定向到 HTTPS,可以在配置文件中添加以下内容:
server {listen 80;server_name yourdomain.com www.yourdomain.com;return 301 https://$host$request_uri; }
这样,所有通过 HTTP 访问的请求都会自动跳转到 HTTPS。
3.4 配置 HTTP/2(可选)
如果你想启用 HTTP/2,可以在 listen
配置项中加入 http2
:
server {listen 443 ssl http2;... }
4. 强化 SSL/TLS 安全性
为了进一步提升 SSL/TLS 配置的安全性,我们可以采取一些最佳实践和强化措施。
4.1 禁用弱加密协议
为了避免使用过时的协议,建议仅启用 TLS 1.2 和 TLS 1.3,禁用 SSLv3 和 TLS 1.0/1.1:
ssl_protocols TLSv1.2 TLSv1.3;
4.2 启用 HTTP Strict Transport Security (HSTS)
HSTS 强制浏览器只能通过 HTTPS 访问网站。你可以通过以下配置启用 HSTS,并设置最长为一年的有效期:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
4.3 启用更强的 Diffie-Hellman 参数
为了提升加密安全性,生成一个更强的 Diffie-Hellman 参数文件:
sudo openssl dhparam -out /usr/local/nginx/ssl/certs/dhparam.pem 2048
然后,在 Nginx 配置中引用该文件:
ssl_dhparam /usr/local/nginx/ssl/certs/dhparam.pem;
4.4 配置加密套件
选择一个现代且安全的加密套件,并禁用已知的弱加密算法:
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
5. 验证配置
完成配置后,我们需要验证 Nginx 配置是否正确,并确保 SSL 正常工作。
5.1 检查 Nginx 配置
首先,检查 Nginx 配置是否有语法错误:
sudo nginx -t
5.2 重启 Nginx
如果配置没有错误,重启 Nginx 使更改生效:
sudo systemctl restart nginx
5.3 测试 HTTPS 配置
你可以通过浏览器访问 https://yourdomain.com
来验证 SSL 是否配置成功。若配置正确,浏览器的地址栏会显示绿色的锁图标,表示安全连接已建立。
6. 定期更新证书
请记住,Let's Encrypt 证书每 90 天需要续期。为确保证书永不过期,可以通过设置自动续期来实现无忧续期。
7.问题排查
1️⃣ 错误:SSL_CTX_set_cipher_list
failed
问题描述:
nginx: [emerg] SSL_CTX_set_cipher_list("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256") failed (SSL: error:1410D0B9:SSL routines:SSL_CTX_set_cipher_list:no cipher match)
原因:
-
Nginx 可能不支持指定的加密套件,或者 OpenSSL 版本不兼容。
解决方案:
-
确保你的 OpenSSL 版本和 Nginx 支持的加密套件兼容。
-
使用支持的加密套件:
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256';
2️⃣ 错误:ssl
parameter requires ngx_http_ssl_module
问题描述:
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:80
原因:
-
Nginx 没有编译
ngx_http_ssl_module
模块,导致无法启用 SSL 配置。
解决方案:
-
安装 Nginx 时确保包含
--with-http_ssl_module
选项:./configure --with-http_ssl_module make make install
-
如果是从源代码编译安装的,检查
nginx -V
确认模块是否已启用。
3️⃣ 错误:The certificate is not trusted because it is self-signed
问题描述:
The certificate is not trusted because it is self-signed. The certificate is not valid for the name 192.168.10.50.
原因:
-
你使用了自签名证书,但浏览器不信任它,因为证书是自己生成的,且与 IP 地址不匹配。
解决方案:
-
临时信任自签名证书:
-
在浏览器中点击 Advanced → Add Exception,然后手动添加信任。
-
-
生成匹配 IP 的自签名证书:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx-selfsigned.key -out nginx-selfsigned.crt -subj "/CN=192.168.10.50"
然后在 Nginx 配置中指定:
ssl_certificate /usr/local/nginx/ssl/certs/nginx-selfsigned.crt; ssl_certificate_key /usr/local/nginx/ssl/private/nginx-selfsigned.key;
-
使用域名而非 IP:
-
在
/etc/hosts
配置 DNS,将域名解析到该 IP,生成证书时使用域名。
-
4️⃣ 错误:The page isn’t redirecting properly
问题描述:
The page isn’t redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
原因:
-
你配置了 HTTP → HTTPS 的重定向,但可能在 HTTPS 配置中又存在错误的重定向,导致无限循环。
解决方案:
-
Nginx 配置分离 HTTP 和 HTTPS 配置:
-
HTTP 重定向:
server {listen 80;server_name www.xxsyyz.com xxsyyz.com;return 301 https://$host$request_uri; }
-
HTTPS 配置:
server {listen 443 ssl;server_name www.xxsyyz.com xxsyyz.com;ssl_certificate /usr/local/nginx/ssl/certs/nginx-selfsigned.crt;ssl_certificate_key /usr/local/nginx/ssl/private/nginx-selfsigned.key;# other configurations... }
-
-
确保没有重复的 HTTP → HTTPS 重定向规则。
5️⃣ 错误:The certificate is not valid for the name 192.168.10.50
问题描述:
-
证书的
CN
(Common Name)不匹配访问的域名或 IP 地址,导致浏览器无法验证证书。
解决方案:
-
修改证书的 CN: 使用 OpenSSL 生成新的证书,并确保 CN 与要访问的域名或 IP 匹配:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx-selfsigned.key -out nginx-selfsigned.crt -subj "/CN=www.xxsyyz.com"
-
使用正确的域名访问: 配置 DNS 或
/etc/hosts
将域名指向该服务器的 IP 地址。
6️⃣ 错误:/bin/sh: ./config: No such file or directory
问题描述:
/bin/sh: line 2: ./config: No such file or directory make[1]: *** [/usr/local/openssl/.openssl/include/openssl/ssl.h] Error 127
原因:
-
Nginx 编译时找不到 OpenSSL 的
config
文件,因为你指向的是已经安装的目录,而不是源码目录。
解决方案:
-
确保
--with-openssl
参数指向的是 OpenSSL 源码目录,而不是已安装的目录:./configure --with-openssl=/usr/local/src/openssl-1.1.1w make make install
7️⃣ 错误:gzip: stdin: not in gzip format
问题描述:
gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
原因:
-
你下载的
.tar.gz
文件实际上是 HTML 页面,而不是压缩包。
解决方案:
-
使用
file
命令确认文件类型:file openssl-1.1.1w.tar.gz
-
如果是 HTML 页面,重新下载正确的 OpenSSL 压缩包,确保下载的文件是
.tar.gz
格式。 -
正确解压
.tar.gz
文件:tar -zxvf openssl-1.1.1w.tar.gz
总结
完成上述配置后,Nginx 服务器将能够通过 HTTPS 提供安全的服务。Let's Encrypt 证书的自动续期功能大幅降低了维护成本,而自签名证书则为开发和测试环境提供了便捷的解决方案。通过启用 HTTP/2 和 HSTS 等现代协议,进一步优化了性能和安全性。