如何在 Nginx 中配置 HTTPS - Linux

一、简介

本文将介绍,在 Ubuntu 系统上为 Nginx 服务器配置 HTTPS 证书。我们将使用 Let's Encrypt 这一广受欢迎的免费证书颁发机构,获取 SSL/TLS 证书。整个流程的核心是 Certbot 工具,通过它自动为域名申请 Let's Encrypt 证书。最后修改 Nginx 的配置文件,进行添加 HTTPS 的支持。

二、安装 certbot

打开终端,执行下面命令,即可完成 certbot 工具的安装。安装完成后,可以使用 version 参数进行验证。

bash 复制代码
# 安装 certbot
sudo apt update # 非必要,根据自己的需求来
sudo apt install certbot
# 验证安装
sudo certbot --version

三、获取证书

获取证书(确保域名 xxxx.yourdomain.com 已解析到服务器),停止占用 80 端口服务(如:Nginx/Apache )。打开终端,执行下面命令,即可即可获取到 xxxx.yourdomain.com 的证书。

bash 复制代码
sudo certbot certonly --standalone   -d xxxx.yourdomain.com   --email youremail@163.com   --agree-tos   --non-interactive

获取的证书将存储在 /etc/letsencrypt/live/xxxx.yourdomain.com 目录下,可以通过下面命令进行查看。

bash 复制代码
sudo ls -la /etc/letsencrypt/live/xxxx.yourdomain.com/

证书有效期:90天;证书类型:仅 DV;推荐续期频率:每60-75天自动续期;续期次数:无限制

四、配置 nginx.cfg

打开 nginx.cfg 配置文件,重点在 # HTTPS 服务器 这行注释下面的 server {...} 其他的,相对简单,可以自行了解。

bash 复制代码
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    # HTTP2 全局配置(新增)
    http2 on;

    # 代理通用配置
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    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-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;

    # 缓冲区配置
    proxy_buffering on;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;

    include /etc/nginx/conf.d/*.conf;

    upstream xxxx_graphql_dev {
        server 127.0.0.1:8089;
    }

    upstream portainer_backend {
        server 127.0.0.1:9000;
    }

    # HTTP 服务器
    server {
        listen 80;
        server_name xxxx.yourdomain.com;

        location / {
            proxy_pass http://diageo_graphql_dev;
            proxy_pass_request_headers on;
            proxy_redirect off;
        }
    }

    # HTTPS 服务器
    server {
        listen 443 ssl;
        server_name xxxx.yourdomain.com;

        # HTTP2 配置(在 listen 指令后使用 http2 参数)
        # listen 443 ssl http2;

        # SSL 证书配置
        ssl_certificate /etc/letsencrypt/live/xxxx.yourdomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/xxxx.yourdomain.com/privkey.pem;
        
        # SSL 安全配置
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
        ssl_prefer_server_ciphers off;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        
        # 由于证书缺少 OCSP 响应,注释掉 OCSP Stapling 配置
        # ssl_stapling on;
        # ssl_stapling_verify on;
        # resolver 8.8.8.8 8.8.4.4 valid=300s;
        # resolver_timeout 5s;

        location /portainer/ {
            proxy_pass http://portainer_backend/;
            proxy_pass_request_headers on;
            proxy_redirect off;
            
            # 确保 WebSocket 在 HTTPS 下正常工作
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        
        location = /portainer {
            return 301 https://$host/portainer/;
        }

        location / {
            proxy_pass http://xxxx_graphql_dev;
            proxy_pass_request_headers on;
            proxy_redirect off;
        }
    }
}
相关推荐
小时前端13 小时前
HTTPS 页面加载 HTTP 脚本被拦?同源代理来救场
前端·https
闲云一鹤1 天前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI3 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing4 天前
WSL+Cpp开发环境配置
linux