Nginx 配置 Https
nginx 添加 --with-http_ssl_module 模块
-
查看Nginx原有的模块
/usr/local/nginx/sbin/nginx -V -
切换至nginx的安装目录,添加http_ssl_module模块
cd /usr/local/download/nginx-1.26.0 # 添加http_ssl_module模块 ./configure --with-http_ssl_module -
备份 原有nginx 配置
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak -
停止nginx
/usr/local/nginx/sbin/nginx -s quit -
编译nginx
make -
将编译好的Nginx覆盖掉原有的Nginx
cp ./objs/nginx /usr/local/nginx/sbin/当系统提示是否覆盖时,输入“yes”即可。
-
查看http_ssl_module模块是否已经加入成功。
/usr/local/nginx/sbin/nginx -V -
Tips
nginx配置https报错:[emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:35
此报错说明缺少nginx: http_ssl_module模块
./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.
这个错误是由于您正在尝试编译nginx并启用HTTP重写模块,但系统缺少PCRE库。有几种解决方案可以解决这个问题:
在CentOS / RedHat上,您可以使用以下命令安装PCRE:
bash sudo yum install pcre pcre-devel
[/usr/local/openssl//.openssl/include/openssl/ssl.h] Error 127 /bin/sh: line 2: ./config: No such file or directory make[1]: *** [/usr/local/ssl/.openssl/include/openssl/ssl.h] Error 127 make[1]: Leaving directory `/usr/local/src/nginx-1.9.9' make: *** [build] Error 2 需要说明的是,我这里编译所使用的Nginx源码是1.9.9的。根据报错信息我们知道,出错是因为Nginx在编译时并不能在/usr/local/ssl/.openssl/ 这个目录找到对应的文件,其实我们打开/usr/local/ssl/这个目录可以发现这个目录下是没有.openssl目录的,因此我们修改Nginx编译时对openssl的路径选择就可以解决这个问题了 解决方案: 打开nginx源文件下的/usr/local/src/nginx-1.9.9/auto/lib/openssl/conf文件: 找到这么一段代码:
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
修改成以下代码:
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
然后再进行Nginx的编译安装即可
配置证书
生成自签名证书
- 生成私钥:
openssl genrsa -out /path/to/your/localhost.key 2048
- 创建证书签名请求 (CSR) 使用刚生成的私钥创建一个证书签名请求 (CSR),并指定 IP 地址作为 CN(Common Name):
openssl req -new -key /path/to/your/localhost.key -out /path/to/your/localhost.csr -subj "/CN=200.116.20.1"
- 创建一个配置文件用于生成自签名证书 创建一个名为 openssl.cnf 的配置文件,内容如下:
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[ req_distinguished_name ]
CN = 200.116.20.1
[ v3_req ]
subjectAltName = @alt_names
[ alt_names ]
IP.1 = 200.116.20.1
将此文件保存到一个方便的位置,例如 /path/to/your/openssl.cnf。
- 生成自签名证书 使用配置文件和私钥生成自签名证书:
openssl x509 -req -in /path/to/your/localhost.csr -signkey /path/to/your/localhost.key -out /path/to/your/localhost.crt -days 365 -extfile /path/to/your/openssl.cnf -extensions v3_req
验证证书 你可以使用 openssl 查看生成的证书信息,确保包含正确的 IP 地址:
openssl x509 -in /path/to/your/localhost.crt -text -noout
你应该在输出中看到 Subject 字段包含
CN=200.116.20.1,以及
X509v3 Subject Alternative Name 字段
包含 IP Address:200.116.20.1。
配置 Nginx 接下来,配置 Nginx 使用生成的自签名证书和私钥。
- 编辑 Nginx 配置文件 将以下配置添加到你的 Nginx 配置文件中:
server {
listen 443 ssl;
server_name 200.116.20.1;
ssl_certificate /path/to/your/localhost.crt; ssl_certificate_key /path/to/your/localhost.key; ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass https://target.example.com;
proxy_ssl_verify off; # 如果目标服务器使用自签名证书,可以关闭验证 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;
}
}
确保将路径 /path/to/your/localhost.crt 和 /path/to/your/localhost.key 替换为实际的文件路径。
- 重启 Nginx 保存配置文件并重启 Nginx:
sudo systemctl restart nginx