1、查询是否已经配置ssl模块
在nginx的安装目录下的./sbin/nginx -v命令,注意是大写v,查看配置是否包含“-with-http_ssl_module”,包含则表示已经配置好ssl,如果不含,需要安装ssl模块。
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.19.5
built by gcc 4.8.5 20150623 (red hat 4.8.5-44) (gcc)
configure arguments: --prefix=/usr/local/nginx
2、安装ssl模块
在nginx安装之前执行./configure的过程中配置上ssl,位置在nginx源安装包的解压的目录下(如果nginx上有配置和页面的话记得在其他地方备份)
[root@localhost nginx-1.19.5]# ls
auto changes changes.ru conf configure contrib html license makefile man objs readme src
[root@localhost nginx-1.19.5]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for os
+ linux 3.10.0-327.el7.x86_64 x86_64
checking for c compiler ... found
+ using gnu c compiler
+ gcc version: 4.8.5 20150623 (red hat 4.8.5-44) (gcc)
checking for gcc -pipe switch ... found
checking for -wl,-e switch ... found
省略中间部分
执行后,再执行编译命令make,仅执行编译命令就行不用执行安装
[root@localhost nginx-1.19.5]# make
make -f objs/makefile
make[1]: entering directory `/root/nginx-1.19.5'
cc -c -pipe -o -w -wall -wpointer-arith -wno-unused-parameter -werror -g -i src/core -i src/event -i src/event/modules -i src/os/unix -i objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
....省略编译部分
这里执行make成功后就不要执行make install命令了,否则nginx会重新安装会将原来的配置覆盖。这里我们看到nginx源解压安装包目录下会生成一个objs目录,把此目录下nginx复制覆盖已经安装好的nginx执行文件
注意:在覆盖移动nginx启动文件的时候,nginx要停掉(./nginx -s stop)
重启之后在查看./nginx -v 已经有ssl模块了
3、 nginx配置ssl证书
在nginx安装目录下的conf下新建一个sslcert目录(目录名字随意),将申请好的证书(可以去阿里云..什么云申请,自行百度)放入
修改nginx.conf
在监听的端口后面追加ssl,并在server中追加如下配置
server_name localhost;
ssl_certificate sslcert/ssl.cer;
ssl_certificate_key sslcert/ssl.key;
ssl_session_timeout 5m;
ssl_protocols tlsv1.2 tlsv1.3;
ssl_ciphers ecdhe-rsa-aes128-gcm-sha256:high:!anull:!md5:!rc4:!dhe;
ssl_prefer_server_ciphers on;
->
ssl的配置也可参考nginx.conf中默认提供的
注意:ssl_certificate证书的后缀不固定,以自身为准,目前知道的有有:.cer、.pem、.crt ;ssl_certificate_key的文件的后缀是固定的为.key
配置完重启即可,会发现原来通过http://访问的不行了,必须通过https://才能访问到
->
发表评论