HTTPS加密

  • HTTPS加密
    • 1. 安装 Nginx 和 Certbot
    • 2. 获取并配置 SSL 证书
    • 3. 配置 Nginx 反向代理
    • 4. 重启 Nginx 并测试
    • 注意事项
    • 添加 A 记录

1. 安装 Nginx 和 Certbot

# 更新包列表
sudo apt update

# 安装 Nginx
sudo apt install nginx

# 安装 Certbot 和 Nginx 插件
sudo apt install certbot python3-certbot-nginx

2. 获取并配置 SSL 证书

注:xxx.com 为网站域名

sudo certbot --nginx -d xxx.com -d www.xxx.com

-d 参数后面跟的是要添加的域名。如果只有一个主域名,只需要指定它;如果有子域或多个域名,需依次列出

Certbot 会自动修改 Nginx 配置文件,添加必要的指令以支持 HTTPS

在过程中,会提示输入电子邮件地址以及同意服务条款等

Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): admin@oearth.online
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf
(Agreed: https://community.letsencrypt.org/tos). You must agree in order to
register with the ACME server. Do you agree?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

Would you be willing, once your first certificate is successfully issued,
to share your email address with the Electronic Frontier Foundation,
a founding partner of the Let's Encrypt project and Backblaze,
our infrastructure partner?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N

3. 配置 Nginx 反向代理

让 Nginx 将请求转发给对应服务应用,需编辑 Nginx 的站点配置文件。通常位于 /etc/nginx/sites-available/ 目录下

server {
    listen 80;
    server_name xxx.com www.xxx.com;

    # 强制所有 HTTP 请求跳转到 HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name xxx.com www.xxx.com;

    ssl_certificate /etc/letsencrypt/live/oearth.online/fullchain.pem; # 确保路径正确
    ssl_certificate_key /etc/letsencrypt/live/oearth.online/privkey.pem; # 确保路径正确

    location / {
        proxy_pass http://localhost:8080; # 假设 对应应用 运行在 8080 端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4. 重启 Nginx 并测试

# 验证是否正确
sudo nginx -t

sudo systemctl restart nginx

注意事项

  • 自动续期:Let's Encrypt 证书的有效期为 90 天,但 Certbot 可以设置定时任务自动续期。通常安装时已经设置了 cron job 或 systemd timer 来自动处理。
  • 防火墙和安全组:确保服务器防火墙和云提供商的安全组允许 443 端口的流量。
  • 强制 HTTPS:上面的 Nginx 配置中包含了将所有 HTTP 请求重定向到 HTTPS 的规则,确保用户总是通过加密连接访问的网站。
  • 多个域名或子域名:需要在DNS上指定对应的域名,否则无法验证是否拥有这个域名

添加 A 记录

添加两条记录(或一条):

类型 主机名 值(IP 地址) TTL
A @ 123.45.67.89 自动
A www 123.45.67.89 自动
  • @ 表示根域名:xxx.come
  • www 表示:www.xxx.come
# HTTP 服务:强制跳转到 HTTPS
server {
    listen 80;
    server_name xxx.com www.xxx.com;
    return 301 https://$host$request_uri;
}

# HTTPS 服务:处理加密请求并代理到 Halo
server {
    listen 443 ssl;
    server_name xxx.com www.xxx.com;

    # SSL 证书(Certbot 自动生成)
    ssl_certificate /etc/letsencrypt/live/oearth.online/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/oearth.online/privkey.pem;

    # 安全设置(推荐)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # 反向代理到 对应服务
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;

        # 超时设置
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
    }

    # 静态资源缓存(可选优化)
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}