温馨提示×

如何在debian用nginxssl

小樊
41
2026-10-08 14:16:40
栏目: 云计算

在 Debian 上用 Nginx + SSL 通常有两种常见方式:

  1. 使用 Let’s Encrypt 免费证书(推荐,自动化)
  2. 使用自签名证书(测试/内网用)

下面分别说明。


一、使用 Let’s Encrypt(正式环境推荐)

1. 安装 Nginx 和 Certbot

sudo apt update
sudo apt install nginx certbot python3-certbot-nginx -y

2. 配置域名 DNS

确保你的域名(如 example.com)已解析到服务器公网 IP。

3. 申请并自动配置 SSL

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

Certbot 会:

  • 自动申请证书
  • 修改 Nginx 配置
  • 启用 HTTPS

4. 自动续期测试

sudo certbot renew --dry-run

Certbot 默认已配置 systemd 定时任务。


二、使用自签名证书(测试用)

1. 生成私钥和证书

sudo openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/ssl/private/nginx-selfsigned.key \
  -out /etc/ssl/certs/nginx-selfsigned.crt

按提示填写信息(Common Name 填你的域名或 IP)。

2. 创建 Nginx SSL 配置

sudo nano /etc/nginx/sites-available/ssl-test

示例配置:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    root /var/www/html;
    index index.html;
}

3. 启用站点

sudo ln -s /etc/nginx/sites-available/ssl-test /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

三、HTTP 强制跳转 HTTPS(推荐)

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

四、常见问题

  • 防火墙:确保 80 / 443 端口开放
  • 权限问题:SSL key 文件权限应为 600
  • 证书不受信任:自签名证书浏览器会警告,属正常现象

如果你愿意,可以告诉我:

  • 是 公网域名 还是 内网/IP
  • Debian 版本(如 11 / 12)
  • 是否已有 Nginx 配置

我可以给你更精确的配置。

0 踩