1、前言
在前段时间我需要在一个https的网站中注入我自己的脚本进行自动填充表单的功能,使用的是JQ的$.post函数进行http请求,但是执行该函数请求后报错如下:
Mixed Content: The page at ‘https://v2020.ft.cntaiping.com:8443/vpn/index.html‘ was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ‘http://192.168.123.79/getKey‘. This request has been blocked; the content must be served over HTTPS.
意思是说当前访问的网站是一个https的网站,发送的请求已被锁定,发送的请求必须是https。那么我们就需要模拟一个https请求来达到我们的目的,方法有很多,这里我们使用nginx模拟一个https代理到我们http的真实请求也可以做到。
2、安装nginx
由于windows安装比较简单,只需要下载好压缩包即可使用,这里主要介绍一下centos7安装nginx 的方法。
2.1下载
nginx下载官网:http://nginx.org/en/download.html
进入官网后可以看到有三个版本Mainline(主要版)、Stable(稳定版)、Legacy(历史版)
我们可以点击稳定版下的nginx-1.24.0,进行下载,windows可以点击nginx/Windows-1.24.0进行下载。
2.2编译并安装
下载好后我们将下载好的nginx-1.24.0.tar.gz上传至/usr/local下,如果没有权限可以自己找一个目录。然后进行解压
tar -zxvf nginx-1.24.0.tar.gz
解压好后进入nginx-1.24.0文件夹。指定编译目录及需要安装的模块。
./configure --prefix=/usr/local/nginx --with-http_ssl_module
注:–prefix是指定编译后安装的文件夹,–with-http_ssl_module则是指定需要安装https反向代理模块,如果不安装此模块那么你将无法反向代理https。
编译源码
make -j 4
注:-j 代表使用几个处理器进行多线程编译,越高越快,但是需要根据自己的处理器进行填写,比如我的处理器是i5-3475s 四核心 所以指定4即可,如果填写超过自己处理器核心可能会报错。
安装
make install
安装好后可以发现之前指定的/usr/local/目录中有已经编译好的nginx目录。
3、安装mkcert
mkcert是谷歌旗下发布的一款自动生成https证书的工具,要比openssl更加快捷方便。
下载:https://github.com/FiloSottile/mkcert/releases
按照自己的系统版本下载即可,我这里下载mkcert-v1.4.4-linux-amd64,下载好后将mkcert-v1.4.4-linux-amd64上传到/usr/local/bin下。修改名称为mkcert。并赋权可执行。
chmod 777 mkcert
然后我们找一个目录存放我们的证书和密钥。我选择/root/mkcert,当然如果使用的是别的用户可以自己挑一个目录存放。
3.1生成https所需证书和密钥
mkcert -cert-file homesystem.pem -key-file homesystem.key 192.168.123.27 homesystem.com
注:-cert-file指定证书名称,-key-file指定密钥名称 192.168.123.27 homesystem.com指定需要代理的https网址的ip和dns,后面的dns名称如果没有可以随便起一个也可以使用你购买的域名。
执行命令后会在当前目录生成homesystem.pem和homesystem.key两个文件。这两个文件需要在nginx的配置文件中使用到。
3.2安装颁发者证书和密钥
由于客户端在安装好生成的证书后会提示“windows没有足够的信息,不能验证该证书”。原因是还需要提供一个颁发者的证书。
mkcert --install
安装好后会在当前用户home目录生成一个.local/share/mkcert目录,我是用的是root账户,所以会生成/root/.local/share/mkcert目录。该目录下会有两个文件rootCA.pem、rootCA-key.pem。
4、配置nginx反向代理https
我们进入之前安装好的nginx配置文件夹下,即/usr/local/nginx/conf
然后编辑nginx.conf文件,在http代理80的server上方增加一个https的server
server {
listen 61231 ssl;
ssl_certificate /root/mkcert/homesystem.pem;
ssl_certificate_key /root/mkcert/homesystem.key;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 4h;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 500M;
location / {
proxy_pass http://192.168.123.27:8099;
}
}
这里代表监听https的61231端口,且将所有请求转发到http://192.168.123.27:8099下
完整的配置文件如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 61231 ssl;
ssl_certificate /root/mkcert/homesystem.pem;
ssl_certificate_key /root/mkcert/homesystem.key;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 4h;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 500M;
location / {
proxy_pass http://192.168.123.27:8099;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
5、使nginx配置生效
在nginx的sbin目录下执行以下命令
./nginx -t -c conf/nginx.conf
如果出现successful代码说明成功
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
5.1nginx常用命令
启动nginx
./nginx
停止nginx
./nginx -s stop
使配置文件生效
./nginx -t -c conf/nginx.conf
6、客户端证书安装
我们将刚刚生成的homesystem.pem和这一次生成的rootCA.pem一起复制到windows客户端,后缀改为crt后即可进行安装。
安装好后我们使用https://192.168.123.27:61231/getKey 即可反向代理到http请求的地址: http://192.168.123.27:8099/getKey 并且不会报错证书不安全等提示