实现https协议请求重写成http协议请求
本文目录
- 实现https协议请求重写成http协议请求
- 1、nginx添加SSL的支持
- 2、生成证书
- OpenSSL 安装
- 使用OpenSSl生成证书
- 生成结果
- 3、配置代理服务器nginx.conf文件
- 4、配置被代理服务器nginx.conf文件
- 5、测试实现
- 6、总结
1、nginx添加SSL的支持
需要在配置时添加https_ssl_moule模块,具体安装步骤请移步
nginx实现不停机添加新模块 && make upgrade的使用
2、生成证书
方式一:使用阿里云/腾讯云等第三方服务进行购买。
首先要申请域名备案成功之后,在领取免费的ssl进行签发后下载nginx对应的zip包
方式二:使用openssl生成证书
学习时,使用openssl生成的单机版证书已经完全可以达到预期的效果,不过真实上线项目,或者是部署微信小程序,就一定需要第三方签发认证的ssl证书
OpenSSL 安装
OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包进行安全通信,并且避免被窃听。
SSL:Secure Sockets Layer安全套接协议的缩写,可以在Internet上提供秘密性传输,其目标是保证两个应用间通信的保密性和可靠性。在Nginx中,如果服务器需要提供安全网页时就需要用到OpenSSL库,所以我们需要对OpenSSL的库文件及它的开发安装包进行一个安装。
可以使用命令yum install -y openssl openssl-devel
来进行安装
安装成功后,可以通过rpm -qa openssl openssl-devel
来查看是否安装成功
上述命令,一个个来的话比较麻烦,我们也可以通过一条命令来进行安装
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
进行全部安装。
使用OpenSSl生成证书
先要确认当前系统是否有安装openssl
[root@localhost conf]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
按照下面的命令进行生成
mkdir /root/cert
cd /root/cert
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
[root@localhost ~]# mkdir /root/cert
[root@localhost ~]# cd /root/cert
[root@localhost cert]# openssl genrsa -des3 -out server.key 1024
Generating RSA private key, 1024 bit long modulus
........................++++++
.++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@localhost cert]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:root
string is too long, it needs to be less than 2 bytes long
Country Name (2 letter code) [XX]:zs
State or Province Name (full name) []:zhengshuang
Locality Name (eg, city) [Default City]:gz
Organization Name (eg, company) [Default Company Ltd]:cs
Organizational Unit Name (eg, section) []:cs1
Common Name (eg, your name or your server's hostname) []:zs_host
Email Address []:1372713212@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:css
[root@localhost cert]# cp server.key server.key.org
[root@localhost cert]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
[root@localhost cert]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=zs/ST=zhengshuang/L=gz/O=cs/OU=cs1/CN=zs_host/emailAddress=1372713212@qq.com
生成结果
[root@localhost cert]# ll
总用量 16
-rw-r--r--. 1 root root 912 6月 1 13:17 server.crt
-rw-r--r--. 1 root root 741 6月 1 13:16 server.csr
-rw-r--r--. 1 root root 887 6月 1 13:17 server.key
-rw-r--r--. 1 root root 963 6月 1 13:17 server.key.org
3、配置代理服务器nginx.conf文件
# HTTPS server
server {
listen 443 ssl;
# ssl on; 同上边443 ssl是一样的
# server_name localhost; 如果是使用第三方签发的ssl,这里需要填写能够ping同的域名
server_name localhost;
# openssl生成密钥的绝对路径,如果是第三方签发的,需要上传到服务器中,并填写相对路径
ssl_certificate /root/cert/server.crt;
ssl_certificate_key /root/cert/server.key;
# 设置会话缓存,shared表示所有的工作进程之间共享缓存,SSL缓存名称,1m缓存大小
ssl_session_cache shared:SSL:1m;
# 5m超时时间,单位为minutes
ssl_session_timeout 5m;
# 允许的密码格式
ssl_ciphers HIGH:!aNULL:!MD5;
# 服务器密码是否优先于客户端密码,设置为on则优先使用服务器端密码
ssl_prefer_server_ciphers on;
location /server9001{
# 地址重写
rewrite ^(.*) http://192.168.137.8:9001$1;
}
}
4、配置被代理服务器nginx.conf文件
server{
listen 9001;
server_name localhost;
default_type text/html;
location /{
return 200 '<h1>192.168.137.8:9001<h1>';
}
}
5、测试实现
6、总结
想要实现https协议请求重写成http协议请求的途径有多种,本文采用的使用nginx的反向代理来实现
- 首选需要让服务器的nginx添加ssl模块支持
- 生成证书的途径,单机版可以使用OpenSSL工具生成,但是如果是要部署上线的项目一定要采用第三方签发授权的ssl证书,推荐阿里云或者腾讯云,签发前必须注册申请域名且成功备案,才可以根据域名去签发ssl证书
- 配置代理服务器的时候,不论是OpenSSL生成的ssl证书还是第三方签发的证书,在配置时的区别仅在于server_name的不同,第三方签发的必须使用域名;使用时保证指定的绝对路径的证书是有效的即可