SSL (Secure Sockets Layer) 及其继任者 TLS (Transport Layer Security) 是用于在互联网上建立加密链接的标准安全技术。通过配置SSL证书,您可以:
在开始配置前,您需要:
获取SSL证书:
准备证书文件:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
# 其他配置...
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
# 启用SSL会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
# 指定协议和密码套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
# 启用OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# HSTS (HTTP Strict Transport Security)
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# 其他安全头
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# 网站根目录和其他配置
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
# Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx
# CentOS/RHEL
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
证书错误:
openssl x509 -in certificate.crt -text -noout
检查证书混合内容警告:
测试工具:
nginx -t
测试配置语法server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;
# ...
}
server {
listen 443 ssl;
server_name another.com;
ssl_certificate /path/to/another.com.crt;
ssl_certificate_key /path/to/another.com.key;
# ...
}
server {
listen 443 ssl;
server_name secure.example.com;
ssl_certificate /path/to/server.crt;
ssl_certificate_key /path/to/server.key;
# 客户端证书验证
ssl_client_certificate /path/to/ca.crt;
ssl_verify_client on;
# 其他配置...
}
通过以上配置,您可以为Nginx服务器建立安全的HTTPS连接,保护用户数据在传输过程中的安全。记得定期检查SSL配置,保持与最新的安全标准一致。