为青龙面板启用 HTTPS (nginx 代理设置)

月朗星稀😚娜不一样 于 2023-09-11 发布 浏览量

青龙面板本身不支持 HTTPS 访问,我们可以通过 nginx 反向代理实现:

配置 HTTPS

使用 nginx -t 命令查看配置文件所在路径。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

然后编辑配置文件,修改或添加以下内容:

server {
    listen               443 ssl;
    ssl_certificate      /home/user/.acme.sh/example.com_ecc/example.com.cer;
    ssl_certificate_key  /home/user/.acme.sh/example.com_ecc/example.com.key;
    ssl_session_timeout  5m;
    ssl_protocols        TLSv1.3;
    http2                on;
    server_name          example.com;

    root                 /usr/share/nginx/html;

    location / {
        #反向代理
        proxy_ssl_server_name  on;
        proxy_pass             http://example.com:5700;
        proxy_set_header       Accept-Encoding '';
        #过滤器模块
        sub_filter             "example.com:5700" "example.com";
        sub_filter_once        off;
    }
}

使用 nginx -t 命令确认配置文件无误后,使用:

nginx -s reload

重启 Nginx 服务使配置生效。

启用 HTTP 自动跳转 HTTPS

如果我们想要实现 HTTP 自动跳转 HTTPS 的功能,我们还可以在配置文件中添加一些重定向的规则:

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

注意这一部分要添加到 server { listen 443; ...}后面。

使用 nginx -t 命令确认配置文件无误后,使用:

nginx -s reload

重启 Nginx 服务使配置生效。