nginx(二):使用自签名证书在Nginx上启用HTTPS

大家好,我是欧阳方超,可以扫描下方二维码关注我的公众号“欧阳方超”,后续内容将在公众号首发。在这里插入图片描述
在这里插入图片描述

1、概述

在当今网络环境中,确保网站安全至关重要。通过在Nginx上配置HTTPS,可以加密与用户之间的数据传输,保护敏感信息不被窃取。本文将详细介绍如何使用自签名证书和私钥在Nginx中启用HTTPS。本文将逐步带领你生成自签名证书、配置Nginx以支持SSL,以及优化TLS协议和加密套件的选择,以确保你的站点既安全又高效。

2、启用HTTP SSL

上一篇介绍如何安装nginx时,未开启ssl,如果要开启的话需要在执行configure时指定–with-http_ssl_module,由于未能找到默认的openssl路径是什么,所以在不指定–with-openssl时,下面的命令会报错:

[root@bogon nginx-1.24.0]# ./configure --prefix=/usr/local/nginx1 --with-http_ssl_module

报错信息如下:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

openssl在机器上可是有的哦, 不信你看:

[root@hadoop102 nginx-1.24.0]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

但是不清楚系统自带的openssl安装目录什么,所以也没办法用–with-openssl指定其目录。于是决定自行安装openssl,但并不打算替换自带的openssl,只是在编译nginx时使用自行安装的openssl。

2.1、安装openssl

自行安装openssl且不覆盖系统自带openssl。指定安装目录为/usr/local/openssl,该目录不是centos7自带openssl所在目录:

[root@hadoop102 openssl-1.1.1w]# ./config --prefix=/usr/local/openssl
Operating system: x86_64-whatever-linux2
Configuring OpenSSL version 1.1.1w (0x1010117fL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)         ***
***                                                                ***
**********************************************************************
[root@hadoop102 openssl-1.1.1w]#

执行make和make install

make
make install

因为没打算替换系统自带的openssl,只是在编译nginx时用新装的openssl,所以对openssl到此为止,不进行创建软连接之类的操作。不过需要更改nginx解压目录中的文件(auto/lib/openssl/conf),确保所使用openssl是刚刚安装的。

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_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"

2.2、安装nginx

现在可以在编译安装 nginx 时,手动指定已编译好的 OpenSSL, --with-openssl=/usr/local/openssl,因为在自行安装openssl时指定的目录为/usr/local/openssl。

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/openssl

执行make,执行过程日志较多,结束时的日志信息如下:

objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_random_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre /usr/local/openssl/lib/libssl.a /usr/local/openssl/lib/libcrypto.a -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/software/nginx-1.24.0”

执行make install

[root@hadoop102 nginx-1.24.0]# make install
make -f objs/Makefile install
make[1]: 进入目录“/software/nginx-1.24.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
        || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
        || mv '/usr/local/nginx/sbin/nginx' \
                '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
        || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
        || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
        || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
        '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
        || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
        || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
        '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
        || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
        '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
        || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/software/nginx-1.24.0”
[root@hadoop102 nginx-1.24.0]#

2.3、启动nginx

由于执行configure命令时指定了prefix为/usr/local/nginx,所以进入该目录进行启动:

[root@hadoop102 nginx]# pwd
/usr/local/nginx
[root@hadoop102 nginx]#
[root@hadoop102 nginx]# sbin/nginx

2.4、查询是否支持ssl

查看此时的nginx是否支持ssl:

[root@hadoop102 ssl]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1w  11 Sep 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/openssl

在输出中可以看到–with-http_ssl_module,则表示Nginx支持SSL。如果没有,则需要重新编译Nginx以支持SSL。

3、自签名证书和私钥生成

在nginx的安装目录下,为即将生成的证书和私钥创建目录ssl,并进入:

[root@hadoop102 nginx]# mkdir ssl
[root@hadoop102 nginx]# cd ssl/

3.1、创建私钥

创建私钥,密码自行指定:

[root@hadoop102 ssl]# openssl genrsa -des3  -out demo.key 1024
Generating RSA private key, 1024 bit long modulus
........................++++++
.....++++++
e is 65537 (0x10001)
Enter pass phrase for demo.key:
Verifying - Enter pass phrase for demo.key:
[root@hadoop102 ssl]# ll
总用量 4
-rw-r--r--. 1 root root 963 912 13:49 demo.key
[root@hadoop102 ssl]#

3.2、创建crs(证书签名请求)

CSR 包含了公钥和一些证书信息,如域名、组织名称等,它是向证书颁发机构 (CA) 申请数字证书的请求文件。在 CA 验证了 CSR 中的信息后,会用私钥对其进行签名,生成最终的数字证书。

[root@hadoop102 ssl]# openssl req -new -key demo.key -out demo.csr
Enter pass phrase for demo.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:MyCompany Corp
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:test.com
Email Address []:test@com.cn

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@hadoop102 ssl]#

3.3、去除私钥的口令

正如前面操作的那样,使用openssl生成私钥demo.key时,会要求输入一个秘密,这个秘密用于保护私钥文件不被他人使用。但是在配置nginx使用ssl时,每次重新加载nginx配置文件时,nginx都会要求输入这个秘密,这样很不方便(注意,这种不方便是相对的,如果安全等级要求较高那就不能去除私钥中的口令)。因此就有了一种操作——使用openssl rsa命令从原始的加密私钥中删除密码保护,生成一个新的私钥文件demo.key(这里将之前的私钥直接覆盖掉了,如果不想覆盖的话可以给新的私钥起个别名)。下面是具体操作,过程中需要输入最初生成私钥时的密码:

[root@hadoop102 ssl]# openssl rsa -in demo.key -out demo.key
Enter pass phrase for demo.key:
writing RSA key

3.5、 生成crt(数字证书文件)

执行如下命令生成crt:

[root@hadoop102 ssl]# openssl x509 -req -days 365 -in demo.csr -signkey demo.key -out demo.crt
Signature ok
subject=/C=CN/ST=Beijin\x08\x08\x08Jing\x08\x08\x08\x08\x08\x08\x08\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x1B[C\x08\x08\x08\x08\x08\x08\x08\x08\x08/L=BeiJing/O=MyCompn\x08any nC\x08\x08C\x1B[C\x08orp/CN=tes.\x08t.com/emailAddress=test@com.\x08\x1B[Ccn
Getting Private key

x509命令用于创建、修改x509证书,days命令用于设置证书有效时间(天数),所以上面的命令将生成一个有效期为365天的自签名证书demo.crt。

可以看到生成的相关文件,起中demo.crt和demo.key就是需要在nginx中配置的文件:

[root@hadoop102 ssl]# ll
总用量 12
-rw-r--r--. 1 root root 1131 912 14:06 demo.crt
-rw-r--r--. 1 root root  794 912 14:05 demo.csr
-rw-r--r--. 1 root root  887 912 14:05 demo.key

4、配置证书及私钥

为nginx配置证书及私钥。打开nginx配置文件,在http块中的server块中,添加以下内容(严格说是改造之前的server块):

server {
        listen       443 ssl;
        #listen       80;
        server_name  localhost;

        ssl_certificate /usr/local/nginx/ssl/demo.crt; #证书路径
        ssl_certificate_key /usr/local/nginx/ssl/demo.key; #私钥路径

        ssl_protocols TLSv1.2 TLSv1.3;  # 启用的TLS版本
        ssl_ciphers HIGH:!aNULL:!MD5;  # 加密套件

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

主要修改点为加入了

listen       443 ssl;

        ssl_certificate /usr/local/nginx/ssl/demo.crt; #证书路径
        ssl_certificate_key /usr/local/nginx/ssl/demo.key; #私钥路径

        ssl_protocols TLSv1.2 TLSv1.3;  # 启用的TLS版本
        ssl_ciphers HIGH:!aNULL:!MD5;  # 加密套件

测试nginx配置

[root@hadoop102 ssl]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重载nginx

[root@hadoop102 ssl]# /usr/local/nginx/sbin/nginx -s reload
[root@hadoop102 ssl]#

访问https网站
现在,可以通过浏览器访问https://192.170.21.52。由于使用的是自签名证书,浏览器会提示安全警告。可以选择继续访问,或者将自签名证书添加到浏览器的信任列表中。
在这里插入图片描述
在这里插入图片描述

5、总结

通过以上步骤,可以成功在Nginx中配置HTTPS并使用自签名证书。虽然自签名证书在生产环境中不推荐使用,但它们非常适合开发和测试环境。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。

参考:
1、https://www.sinosky.org/compile-nginx-with-a-custom-openssl-library/
2、https://ocdman.github.io/2018/11/15/OpenSSL%E5%8D%87%E7%BA%A7%E5%90%8E%E5%A6%82%E4%BD%95%E5%8D%87%E7%BA%A7Nginx/
3、https://openssl-library.org/source/old/1.1.1/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值