Nginx 配置支持 HTTPS 代理

个人博客地址:Nginx 配置支持 HTTPS 代理 | 一张假钞的真实世界

本文描述的是Nginx HTTPS反向代理的情况(即后端服务是HTTP的)。

使用openssl配置ssl证书

生成服务器端的私钥(key 文件):

bash 复制代码
# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.............+++
..........+++
e is 65537 (0x10001)
Enter pass phrase for server.key:

输入两次密码后key文件生成完毕。

生成CSR(Certificate Signing Request)文件:

bash 复制代码
# openssl req -new -key server.key -out server.crs

根据提示输入需要的信息后CSR文件生成完毕。

生成自签名的CA文件:

bash 复制代码
# openssl x509 -req -days 3650 -in server.crs -signkey server.key -out ca.crt
Signature ok
subject=/C=cn/ST=beijing/L=beijing/O=test/OU=test/CN=test/emailAddress=test@test.com
Getting Private key
Enter pass phrase for server.key:

输入key文件的密码后CA文件生成完毕。

Nginx配置

bash 复制代码
server {
    listen 443;
    ssl on;
    ssl_certificate ca.crt;
    ssl_certificate_key server.key;

    location / {
        proxy_pass http://192.168.36.144:11000;
     }
}

重新加载Nginx配置文件后,发现Nginx代理的443端口未正常启动。查看Nginx error日志发现以下异常信息:

bash 复制代码
2019/03/12 16:49:26 [emerg] 85175#0: SSL_CTX_use_PrivateKey_file("/etc/nginx/server.key") failed (SSL: error:0906406D:PEM routines:PEM_def_callback:problems getting password error:0906A068:PEM routines:PEM_do_header:bad password read error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib)

通过以下方式解决:

bash 复制代码
# mv server.key server.key.org
# openssl rsa -in server.key.org -out server.key

重新加载Nginx配置文件。此时也不需要再输入key文件的密码了。

相关推荐
袁泽斌的学习记录39 分钟前
ubuntu22.04安装cuda11.4版本
linux·运维·服务器
荣光波比43 分钟前
Docker(一)—— Docker入门到精通:从基础概念到容器管理
运维·docker·容器·云计算
m0_4646082644 分钟前
Docker入门
运维·docker·容器
游戏开发爱好者81 小时前
Nginx HTTPS 深入实战 配置、性能与排查全流程(Nginx https
运维·nginx·ios·小程序·https·uni-app·iphone
小闫BI设源码1 小时前
Docker的介绍
运维·docker·容器·数据持久化·端口映射·卷挂载·配置外挂
万事可爱^1 小时前
如何在云服务器上使用LLaMA Factory框架微调模型
运维·服务器·llm·sft·llama·模型微调·ai agent
新手小白*2 小时前
Nginx反向代理与缓存功能-第一章
运维·nginx
Lin_Aries_04212 小时前
基于 GitLab 的自动化镜像构建
linux·运维·docker·容器·自动化·gitlab
运维栈记2 小时前
自动化运维利器:MCP Server + 阿里云
运维·自动化
namekong83 小时前
ubuntu 通过下面几种方式查看系统 重启时间/开机时间:
linux·运维·ubuntu