Preview整个网站还在搭建中,当前包含较多草稿和未完成内容,暂未正式发布。
部署上线

HTTPS 证书配置

为网站启用 HTTPS 加密连接

HTTPS 是现代网站的标配,不仅保护用户数据,还能提升 SEO 排名。

为什么需要 HTTPS

  • 数据加密:防止中间人攻击
  • 用户信任:浏览器显示"安全"标识
  • SEO 优势:Google 优先排名 HTTPS 网站
  • 必需功能:PWA、地理定位等功能要求 HTTPS

托管平台自动配置

大多数托管平台自动配置 HTTPS,无需手动操作。

Vercel

  • 自动为所有域名配置 Let's Encrypt 证书
  • 自动续期
  • 强制 HTTPS 重定向

Cloudflare Pages

  • 自动配置 Cloudflare Universal SSL
  • 支持自定义证书
  • 自动续期

Railway / Fly.io

  • 自动配置 Let's Encrypt 证书
  • 添加自定义域名时自动生成

手动配置(自建服务器)

如果使用自己的服务器,需要手动配置。

使用 Certbot(Let's Encrypt)

# 安装 Certbot
sudo apt install certbot python3-certbot-nginx

# 为 Nginx 配置证书
sudo certbot --nginx -d example.com -d www.example.com

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

Nginx 配置

server {
    listen 80;
    server_name example.com www.example.com;
    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;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

证书类型

DV(域名验证)

  • 验证域名所有权
  • 几分钟内签发
  • 免费(Let's Encrypt)
  • 适合大多数网站

OV(组织验证)

  • 验证组织身份
  • 1-3 天签发
  • 付费($50-200/年)
  • 适合企业网站

EV(扩展验证)

  • 严格验证组织
  • 浏览器显示公司名称
  • 付费($200-500/年)
  • 适合金融、电商

对于 MVP,DV 证书完全够用。

强制 HTTPS

确保所有 HTTP 请求重定向到 HTTPS。

Vercel

vercel.json 中:

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains"
        }
      ]
    }
  ]
}

Next.js

next.config.js 中:

module.exports = {
  async redirects() {
    return [
      {
        source: '/:path*',
        has: [
          {
            type: 'header',
            key: 'x-forwarded-proto',
            value: 'http',
          },
        ],
        destination: 'https://example.com/:path*',
        permanent: true,
      },
    ]
  },
}

检查 HTTPS 配置

在线工具

命令行

# 检查证书
openssl s_client -connect example.com:443

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

常见问题

混合内容警告

页面通过 HTTPS 加载,但引用了 HTTP 资源(图片、脚本)。

解决:

  • 将所有资源改为 HTTPS
  • 使用相对路径或协议相对路径(//example.com/image.jpg

证书过期

Let's Encrypt 证书 90 天过期,需要自动续期。

检查自动续期:

sudo certbot renew --dry-run

证书不受信任

  • 检查证书链是否完整
  • 确认使用了正确的中间证书
  • 清除浏览器缓存

安全最佳实践

启用 HSTS

强制浏览器使用 HTTPS:

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

禁用旧协议

只允许 TLS 1.2 和 1.3:

ssl_protocols TLSv1.2 TLSv1.3;

使用强加密套件

ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;

免费证书提供商

  • Let's Encrypt:最流行,自动化
  • ZeroSSL:Let's Encrypt 的替代品
  • Cloudflare:使用 Cloudflare 时自动提供

下一步

目录