1. 程式人生 > 其它 >nginx 配置https

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