配置nginx代理实现https访问

Google宣布从Chrome网络浏览器的下一个稳定版本(90)开始,将为地址栏中输入的所有URL网址默认选择HTTPS协议。这意味着未启用HTTPS的网站的打开速度将受到影响。
对于尚不支持HTTPS的网站,当HTTPS尝试失败(包括名称不匹配或不受信任的自签名证书之类的证书错误,或DNS解析失败的连接错误)时,Chrome会退回到HTTP。

一、nginx安装

1、 下载安装包

mkdir /opt/nginx
cd  /opt/nginx
wget http://nginx.org/download/nginx-1.20.2.tar.gz
wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz

2、 安装c++编译环境

sudo yum install gcc-c++

3、安装perl(非必须,如果安装OpenSSL时提示You need Perl 5,则需安装)

tar -xzf perl-5.30.1.tar.gz
cd perl-5.30.1
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install

4、 安装OpenSSL

tar -zxvf openssl-fips-2.0.10.tar.gz 
cd openssl-fips-2.0.10/
./config && make && make install

5、 安装pcre

tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40/
./configure && make && make install
yum -y install openssl openssl-devel

6、 安装zlib

tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure && make && make install

7、 安装nginx

tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2/
./configure --with-http_ssl_module  && make && make install

8、 启动nginx

[root@hhh nginx-1.20.2]#  whereis nginx
nginx: /usr/local/nginx
[root@hhh  nginx]# cd /usr/local/nginx/
[root@hhh  nginx]# ls
conf  html  logs  sbin
[root@hhh  nginx]# /usr/local/nginx/sbin/nginx 
[root@hhh  nginx]#

9、 nginx基本操作

启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
验证配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
查看nginx进程
[root@x86-103 nginx]# ps -aux | grep nginx
root     2814713  0.0  0.0  32980   392 ?        Ss   14:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   2814714  0.0  0.2  66588  4216 ?        S    14:38   0:00 nginx: worker process
root     2843725  0.0  0.0  12112  1112 pts/1    R+   14:39   0:00 grep --color=auto nginx

二、配置反向代理

编辑nginx.conf文件
文件中找到“HTTPS server”字样,将下方注释放开,按照如下配置,配置完成后重启nginx。
在这里插入图片描述

三、获取SSL证书

从阿里云获取免费SSL证书
1、 购买域名(top域名首年1元)。
在这里插入图片描述
2、 购买完成域名后可0元购买一年ssl证书
在这里插入图片描述
3、 创建证书-证书申请
在这里插入图片描述
在这里插入图片描述
4、 配置域名解析dns
在这里插入图片描述
5、 验证-提交审核
一般很快即可审核成功
在这里插入图片描述
6、 下载SSL证书文件
选择NGINX进行下载,即可获得pem和key文件
在这里插入图片描述
在这里插入图片描述

四、https访问

一定要保证,证书申请时填写的域名、dns配置的域名解析,以及本地nginx使用的证书文件一致,并且最终访问时使用填写的域名访问,证书才有效,否则就会出现下面的情况(证书无效)。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐