记一次正向代理使用

背景

最近项目在接一家支付机构,支付机构的接口调用有ip的白名单限制,因此在这里我遇到了几个头疼的问题

  • 我司的网络出口ip不固定 导致ip白名单无法生效
  • 支付机构的调用地址是https 因此无法直接使用nginx来正向代理

解决过程

为了解决这个问题,通过网络上获取的知识了解到,nginx的http_proxy_connect模块可以转发https请求
因此只需要通过编译nginx的模块即可

  1. 找到http_proxy_connect的patch包 https://github.com/chobits/ngx_http_proxy_connect_module#select-patch

  2. 根据文档进行编译 (openresty也是这样编译的)
    在nginx目录下 首先克隆项目 然后执行如下命令即可

    $ patch -p1 < ./ngx_http_proxy_connect_module/patch/proxy_connect.patch
    $ ./configure --add-dynamic-module=./ngx_http_proxy_connect_module
    $ make && make install
  3. 在nginx.conf配置中增加一行(尽可能在第一行增加 后面增加会抛时机不对的错误)

    load_module   /usr/local/nginx/modules/ngx_http_proxy_connect_module.so;
  4. 增加代理服务器配置
    新增forward.conf文件 内容如下

    server {
     listen  9468;
     # dns resolver used by forward proxying
     resolver 8.8.8.8;
     # 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;
     }
    }
  5. 宿主机命令使用
    x.x.x.x需要换成nginx所在的机器

    export  http_proxy=x.x.x.x:9468
    export  https_proxy=x.x.x.x:9468
  6. postman中使用
    在设置中找到proxy 添加Add a custom proxy configuration即可

  7. 宿主机全局代理

ubuntu系统可以在设置中主动设置网络代理开启