可参考链接:
https://blog.csdn.net/weixin_43398820/article/details/102736734
安装过程中可能会出现下面的问题:
[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
可以执行下面命令:
yum -y install gcc gcc-c++ autoconf automake make
./configure
[root@ns3129983 sbin]# ./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
可以执行下面的命令:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx -s reload
--with-http_stub_status_module --with-http_ssl_module
查看当前是否已经安装该模块(大写的 -V):
[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
如果没有最后一个就需要先安装改模块
安装之前需要先下载几个库:
yum install gcc
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
找到解压缩后的nginx下载目录:
[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
然后执行下面命令
./configure --with-http_stub_status_module --with-http_ssl_module
make
make命令如果失败很可能是上面下载的几个库的问题
cp ./objs/nginx /usr/local/nginx/sbin/
./nginx -V
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