HTTP vs HTTPS:差别远比你想的大

HTTPS(HyperText Transfer Protocol Secure)是HTTP的加密版本。当你访问一个使用HTTPS的网站时,浏览器和服务器之间的所有通信都经过TLS(Transport Layer Security)协议加密,任何第三方都无法窃听或篡改传输的数据。

截至2025年底,全球排名前100万的网站中有超过95%已启用HTTPS。Google Chrome自2018年起就将所有HTTP网站标记为"不安全"。如果你的网站还在使用HTTP,是时候升级了。

HTTPS的工作原理

TLS握手过程

客户端                              服务器
  |                                   |
  |--- ClientHello (支持的TLS版本、   |
  |    密码套件列表) ------->         |
  |                                   |
  |<------- ServerHello (选定的       |
  |         密码套件、证书) ----------|
  |                                   |
  |--- 验证证书、生成预主密钥         |
  |    用服务器公钥加密 -------->     |
  |                                   |
  |<------- 双方计算会话密钥 -------->|
  |                                   |
  |====== 加密通信开始 ===============|

TLS 1.3 的改进

TLS 1.3(RFC 8446)相比TLS 1.2有显著改进:

特性TLS 1.2TLS 1.3
握手往返次数2-RTT1-RTT
0-RTT恢复不支持支持
前向保密可选强制
废弃的算法支持RC4等已移除
握手加密明文加密
性能基准提升约30%

SSL证书类型

按验证级别分类

证书类型英文验证内容价格签发时间适用场景
域名验证DV域名所有权免费-低分钟级个人/小型网站
组织验证OV域名+组织身份中等1-3天企业网站
扩展验证EV严格身份审查1-2周金融/电商

对于大多数网站,DV证书已经完全够用。Let’s Encrypt提供的免费DV证书是最受欢迎的选择。

使用Let’s Encrypt获取免费证书

安装Certbot

# Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx -y

# CentOS/RHEL
sudo dnf install certbot python3-certbot-nginx -y

获取并配置证书

# 为Nginx获取证书
sudo certbot --nginx -d example.com -d www.example.com

# 或者仅获取证书不自动配置
sudo certbot certonly --webroot -w /var/www/html -d example.com

# 测试自动续期
sudo certbot renew --dry-run

设置自动续期

# 创建定时任务
# /etc/cron.d/certbot-renew
0 0,12 * * * root certbot renew --quiet --deploy-hook "systemctl reload nginx"

Nginx HTTPS最佳配置

server {
    listen 80;
    server_name example.com www.example.com;
    # 强制跳转HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    # 证书文件
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # TLS配置
    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;
    ssl_prefer_server_ciphers off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    # 安全头
    add_header X-Frame-Options DENY always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Content-Security-Policy "default-src 'self'" always;

    # ... 其他配置
}

验证HTTPS配置

部署完证书后,使用以下工具验证你的HTTPS配置质量:

# 使用openssl测试连接
openssl s_client -connect example.com:443 -servername example.com

# 检查证书信息
openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -text

# 检查证书到期时间
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -enddate

还可以使用在线工具SSL Labs(ssllabs.com/ssltest)进行全面测试,目标是获得A+评级。

常见问题与排错

混合内容警告

部署HTTPS后,如果页面中仍有HTTP资源引用(图片、CSS、JS),浏览器会显示混合内容警告。解决方法是将所有资源引用改为HTTPS或使用协议相对URL。

证书链不完整

有时服务器只配置了站点证书而缺少中间证书,导致某些客户端无法验证。确保使用fullchain.pem而非cert.pem

关于更全面的服务器安全配置,可以参考我们的服务器安全加固清单Linux服务器安全配置指南。

总结

HTTPS不再是可选项,而是每个网站的基本要求。借助Let’s Encrypt的免费证书和Certbot的自动化工具,部署HTTPS从未如此简单。立即行动,为你的网站加上这把安全锁。