- 反向代理https
-
若代理出口为http,正常proxy_pass代理即可
如:代理https://www.baidu.com/
......
listen 80;
resolver 114.114.114.114 valid=30s;
......
location / {
proxy_pass https://www.baidu.com;
}
......
访问http://localhost即可代理到https://www.baidu.com
- 代理出口为https
listen 443 ssl;
server_name localhost;
resolver 114.114.114.114 valid=30s;
#charset koi8-r;
#access_log logs/host.access.log main;
ssl on;
# SSL cert files ###
ssl_certificate D:/develop/nginx-1.14.2/ssl/aoshiwei.com.crt;
#由于这个证书是自己手动颁发的,是不受信任的,访问时会有个“大叉”提示,但是不影响访问
ssl_certificate_key D:/develop/nginx-1.14.2/ssl/aoshiwei.com.key; #如果是线上环境,可以购买被信任后的证书,拷贝过来使用。
ssl_session_timeout 5m;
location / {
# proxy_pass https://api.weixin.qq.com;
proxy_pass https://www.baidu.com;
}
访问https://localhost即可,由于证书是自己制作的不受信任,点击高级、继续访问即可
正向代理https
nginx默认是没有加载https的代理模块,通过打补丁的方式,然后编译安装就可以,此出我用的系统是centos7.x,windows暂未尝试
- 下载nginx源码包,https模块,打补丁,编译,编译安装
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -xf nginx-1.14.2.tar.gz
cd nginx-1.14.2/
#安装git
yum install -y git
#下载https模块
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
#安装patch
yum install -y patch pcre pcre-devel
patch -p1 < ngx_http_proxy_connect_module/patch/proxy_connect_1.14.patch
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=ngx_http_proxy_connect_module
#make可能会报错,若报错 yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel 并重新执行上面的./configure
make && make install
#指向配置文件
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
- 正向代理配置
vim /usr/local/nginx/conf/nginx.conf
......
#配置文件
server {
listen 443;
# dns resolver used by forward proxying
resolver 223.5.5.5;
# forward proxy for CONNECT request
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
# forward proxy for non-CONNECT request
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
}
}
......
#启动测试并启动
nginx -t
nginx
完成服务配置
- 访问方式,以chrome访问为例
打开设置,设置代理服务器
设置刚才配置的nginx代理的地址端口,点击保存
然后chrome百度搜索 IP地址(或者cmd输入 curl ifconfig.me),可以看到搜索出来的ip公网出口为nginx的服务器出口地址,此方式可以自己在国外服务器自己搭建nginx代理访问xx网站。