nginx正向代理

来源:互联网 发布:100~200的质数c语言 编辑:程序博客网 时间:2024/05/22 00:52

一、nginx正向代理,配置方式如下 

server{
        resolver 114.114.114.114;             #本地DNS      
        listen 8888; 
        location / {#正向代理       
         proxy_pass $scheme://$http_host$request_uri;
        }
   }

使用java作为客户端使用方式

1、设置系统运行参数

System.setProperty("http.proxyHost","127.0.0.1");

System.setProperty("http.proxyPort","9999");

System.setProperty("https.proxyHost","127.0.0.1");

System.setProperty("https.porxyPort",”9999");

 

2、通过设置虚拟机参数

-Dhttp.proxyHost=172.31.251.6 -Dhttp.proxyPort=8888-Dhttps.proxyHost=172.31.251.6 -Dhttps.proxyPort=8888

 

测试结果是http能够通过正向代理正常访问,https不能正常访问,返回信息CONNECTwww.baidu.com:443 HTTP/1.1" 400 172。

 

查了不少资料,才知nginx不支持正向代理https请求,但是可以利用nginx反向代理动态传参的特性实现类似的功能,透明穿透访问。

二、Nginx反向代理,动态传参替换方案

nginx配置文件nginx.conf 做如下配置

server {
        listen       8888 ;
        server_name  localhost;
        location /gateway {
           proxy_pass  $arg_path;
           access_log  logs/proxyAccess.log main;
           break;
        }
}

 

通过请求地址:http://127.0.0.1:8888/gateway?path=https://www.baidu.com实现动态传参,实现透明的https代理请求。

 


0 0
原创粉丝点击