nginx正向代理、反向代理https

- 反向代理https

  1. 若代理出口为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
  1. 代理出口为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暂未尝试

  1. 下载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
  1. 正向代理配置
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

完成服务配置

  1. 访问方式,以chrome访问为例
    打开设置,设置代理服务器
    在这里插入图片描述
    设置刚才配置的nginx代理的地址端口,点击保存
    在这里插入图片描述

然后chrome百度搜索 IP地址(或者cmd输入 curl ifconfig.me),可以看到搜索出来的ip公网出口为nginx的服务器出口地址,此方式可以自己在国外服务器自己搭建nginx代理访问xx网站。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值