温馨提示×

Debian Nginx如何启用HTTPS

小樊
36
2026-01-03 22:55:58
栏目: 智能运维

要在Debian上使用Nginx启用HTTPS,请按照以下步骤操作:

  1. 安装Certbot和Nginx: 如果您还没有安装Certbot和Nginx,请先安装它们。打开终端,然后运行以下命令:

    sudo apt update
    sudo apt install nginx certbot python3-certbot-nginx
    
  2. 获取SSL证书: 使用Certbot获取SSL证书。运行以下命令,将yourdomain.com替换为您的域名:

    sudo certbot --nginx -d yourdomain.com
    

    Certbot将自动配置Nginx以使用HTTPS,并将SSL证书文件存储在/etc/letsencrypt/live/yourdomain.com/目录中。

  3. 配置Nginx: Certbot将自动修改您的Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.com),以启用HTTPS。请确保配置文件中的以下部分正确设置:

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
        location / {
            return 301 https://$host$request_uri;
        }
    }
    
    server {
        listen 443 ssl;
        server_name yourdomain.com www.yourdomain.com;
    
        ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
        # 其他配置...
    }
    

    如果您对Nginx配置进行了自定义更改,请确保在更改后重新加载Nginx以使更改生效:

    sudo nginx -t
    sudo systemctl reload nginx
    
  4. 测试HTTPS连接: 打开浏览器,访问https://yourdomain.com。您应该看到一个安全的连接,浏览器地址栏中显示一个绿色的锁图标。

现在,您已经成功在Debian上使用Nginx启用了HTTPS。

0