Nginx的安裝及配置https訪問

安裝nginx

可參考連接:
http://www.javashuo.com/article/p-kfofazoh-ne.html
安裝過程當中可能會出現下面的問題:html

執行 ./configre報錯

[root@ns3129983 nginx-1.14.0]# ./configure
checking for OS
 + Linux 3.10.0-1062.12.1.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

能夠執行下面命令:nginx

yum -y install gcc gcc-c++ autoconf automake make
./configure

重啓nginx出錯

[root@ns3129983 sbin]# ./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

能夠執行下面的命令:c++

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx -s reload

要想配置https訪問,還要下載模塊:

--with-http_stub_status_module --with-http_ssl_moduleweb

查看當前是否已經安裝該模塊(大寫的 -V):bash

[root@ip-172-31-17-161 sbin]# ./nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module

若是沒有最後一個就須要先安裝改模塊服務器

安裝以前須要先下載幾個庫:session

yum install gcc
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel

找到解壓縮後的nginx下載目錄:tcp

[root@ip-172-31-17-161 src]# cd nginx-1.14.0
[root@ip-172-31-17-161 nginx-1.14.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src

而後執行下面命令svg

./configure --with-http_stub_status_module --with-http_ssl_module
make

make命令若是失敗極可能是上面下載的幾個庫的問題ui

cp ./objs/nginx /usr/local/nginx/sbin/
./nginx -V

配置nginx.conf

https訪問須要配置證書,下載兩個證書應該放在nginx.conf 文件的同級目錄,通常是在
/usr/local/nginx/conf 目錄下(能夠在配置文件中指定文件的絕對路徑)
兩個文件是以.pem和.key後綴的文件(有的能夠直接下載,有的證書能夠經過openssl生成),例如:

example.com.pem和example.com.com.key
找到nginx.conf修改下面配置:

# HTTPS server2
    server {
        listen       443 ssl;
        #example.com當前服務器的域名
        server_name  example.com;
		
		#下面是兩個證書
        ssl_certificate      example.com.pem;
        ssl_certificate_key  example.com.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;
        
			location /{         
				proxy_pass              http://127.0.0.1:8080/;    
				proxy_set_header        Host $host:$server_port;
				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;
				client_max_body_size    100m;
			}
    }

能夠查看配置是否正確

[root@ns3129983 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

注:必定不要忘記把443端口打開不然訪問的時候會報下面的錯誤:
PR_END_OF_FILE_ERROR

在這裏插入圖片描述

查看當前開放的端口:

iptables-save;

在這裏插入圖片描述
若是沒有任何輸出表示沒有打開防火牆,若是有輸出注意看最後幾行會有當前開放的端口狀況:

若是須要開放端口443輸入下面命令:

firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
iptables-save;

配置完後重啓nginx

cd /usr/local/nginx/sbin
./nginx -s reload

能夠經過https訪問url了 https://example.com