nginx 配置https

前提条件,nginx已经搭建成功

配置步骤:

1.Nginx的ssl模块安装

在配置ssl证书之前,要确保你的nginx已经安装了ssl模块,一般情况下自己安装的nginx都是不存在ssl模块的。

这里先检查下自己是否存在ssl模块:

进入到你的nginx安装目录下面,我的目录是在(/usr/local/nginx),如果你的nginx安装步骤和上面的文章一致的话,那你的目录和我应该是一致的

进入到目录的sbin目录下,输入  ./nginx -V 

[root@zp-host-nginx sbin]# ./nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments:

如果出现 (configure arguments: --with-http_ssl_module) ,则表示已经安装ssl模块,如果没有出现则安装ssl模块;

安装步骤:

1.进入nginx解压目录,执行命令 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

[root@zp-host-nginx nginx]# cd /root/nginx-1.12.2
[root@zp-host-nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for OS
 + Linux 3.10.0-1160.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
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
.......

2. 接下来执行 命令 make 进行编译 ,千万不要执行,千万不要进行make install,否则就是覆盖安装

[root@zp-host-nginx nginx-1.12.2]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.12.2'
............

上述操作执行完成以后,你的目录下会出现objs文件夹,文件夹存在nginx文件,如图:

 

 3.停止Nginx,正常命令直接 nginx -s stop就可以

[root@zp-host-nginx nginx]# cd /usr/local/nginx/sbin/
[root@zp-host-nginx sbin]#
[root@zp-host-nginx sbin]# ./nginx -s stop
[root@zp-host-nginx sbin]#

4. 替换之前的nginx

[root@zp-host-nginx sbin]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@zp-host-nginx nginx]# cp /root/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
[root@zp-host-nginx nginx]#

5.进入到nginx安装目录下,查看ssl时候成功

[root@zp-host-nginx sbin]# ./nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@zp-host-nginx sbin]#

 2.配置ssl证书

解压缩下载好的证书(证书一般是pem文件和key文件,这里名字可以随便改)

1.将下载好的证书上上传到服务器

[root@zp-host-nginx ~]# mkdir cert
[root@zp-host-nginx ~]# cd cert/
[root@zp-host-nginx cert]#
[root@zp-host-nginx cert]# ls -l
total 4
-rw-r--r--. 1 root root 3240 Dec 8 01:29 test-x509.pem
[root@zp-host-nginx cert]#

进入nginx目录下,配置nginx.conf 文件

[root@zp-host-nginx conf]# cd /usr/local/nginx/conf
[root@zp-host-nginx conf]#
[root@zp-host-nginx conf]# ls -l
total 60
-rw-r--r--. 1 root root 1077 Dec  7 10:29 fastcgi.conf
-rw-r--r--. 1 root root 1077 Dec  7 10:29 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Dec  7 10:29 fastcgi_params
-rw-r--r--. 1 root root 1007 Dec  7 10:29 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Dec  7 10:29 koi-utf
-rw-r--r--. 1 root root 2223 Dec  7 10:29 koi-win
-rw-r--r--. 1 root root 3957 Dec  7 10:29 mime.types
-rw-r--r--. 1 root root 3957 Dec  7 10:29 mime.types.default
-rw-r--r--. 1 root root 2656 Dec  7 10:29 nginx.conf
-rw-r--r--. 1 root root 2656 Dec  7 10:29 nginx.conf.default
-rw-r--r--. 1 root root  636 Dec  7 10:29 scgi_params
-rw-r--r--. 1 root root  636 Dec  7 10:29 scgi_params.default
-rw-r--r--. 1 root root  664 Dec  7 10:29 uwsgi_params
-rw-r--r--. 1 root root  664 Dec  7 10:29 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Dec  7 10:29 win-utf
[root@zp-host-nginx conf]# vim nginx.conf

增加以下内容

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
server {
    #监听443端口
    listen 443;
    #你的域名
    #server_name huiblog.top;
    ssl on;
    #ssl证书的pem文件路径
    ssl_certificate  /root/card/test-x509.pem;
    #ssl证书的key文件路径
    #ssl_certificate_key /root/card/huiblog.top.key;
    location / {
     proxy_pass  http://公网地址:项目端口号;
    }
}
server {
    listen 80;
   #server_name huiblog.top;
    #将请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
      }
    }

注意:这里需要在安全组中开放443端口。

3.重启nginx

 

posted @ 2021-12-08 09:25  为什么要取名字  阅读(91)  评论(0编辑  收藏  举报