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