Nginx之ngx_http_proxy_connect_module模块

近期由于项目需要使用到https正向代理,而nginx官方模块仅支持做http正向代理,一番百度学习后发现了该模块,故今日记录下此笔记供大家一起学习交流

ngx_http_proxy_connect_module模块主要用于隧道SSL请求的代理服务器

GitHub地址:http://www.github.com/chobits/ngx_http_proxy_connect_module

nginx配置:

server {
                    listen 3300;
                    resolver 114.114.114.114    ipv6=off;

                    proxy_connect;
                    proxy_connect_allow     443 563;
                    proxy_connect_connect_timeout   30s;
                    proxy_connect_read_timeout      30s;
                    proxy_connect_send_timeout      30s;

                    location / {
                                    proxy_pass      http://$host;
                                    proxy_set_header        Host $host;
                    }
 }

通过上配置,可以通过HTTP连接隧道获得任何https网站。

下面可以本地通过命令 curl进行简单测试

curl https://www.sina.com/-v -x 127.0.0.1:3300

也可以将浏览器配置为将这里 Nginx 用作代理服务器 ( 比如 Google Chrome HTTP代理设置)


Nginx 版本启用重写阶段补丁

1.4.x ~ 1.12.x 不是 proxy_connect.patch
1.4.x ~ 1.12.x 是的 proxy_connect_rewrite.patch
1.13.x ~ 1.14.x 不是 proxy_connect_1014.patch
1.13.x ~ 1.14.x 是的 proxy_connect_rewrite_1014.patch

proxy_connect.patch 包含宏NGX_HTTP_RPOXY_CONNECT中的逻辑,而配置脚本将自动启用这里宏。

默认情况下,MODULE 禁用连接请求的Nginx 重写阶段,这意味着不能使用 if。set。rewrite_by_lua 和其他重写阶段指令。 要启用这些功能,你应该使用 proxy_connect_rewrite.patch 而不是 proxy_connect.patch。

Nginx安装方法

$ wget http://nginx.org/download/nginx-1.15.0.tar.gz
$ tar -xzvf nginx-1.15.0.tar.gz
$ cd nginx-1.15.0/
$ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
$ ./configure --add-module=/path/to/ngx_http_proxy_connect_module
$ make && make install

OpenResty安装方法

$ wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
$ tar -zxvf openresty-1.13.6.2.tar.gz
$ cd openresty-1.13.6.2
$ ./configure --add-module=/path/to/ngx_http_proxy_connect_module
$ patch -d build/nginx-1.13.6/ -p 1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1014.patch
$ make && make install

指令详解

proxy_connect

语法:proxy_connect
默认值:none
上下文:server
启用"连接"http方法支持。

proxy_connect_allow

语法: proxy_connect_allowall | [port.. .] | [port-range.. .]
默认值:443 563
上下文:server
这里指令指定代理连接方法可以连接的端口号或者范围的列表。
默认情况下,只启用默认的https端口( 443 ) 和默认的snews端口( 563 )。
使用这里指令将覆盖这里默认值,并允许仅连接到所列端口。
值 all 将允许所有端口代理。
值 port 将允许指定的端口代理。
port-range 将允许指定的端口范围进行代理,例如:

proxy_connect_allow 1000-2000 3000-4000; # allow range of port from 1000 to 2000, from 3000 to 4000.

proxy_connect_connect_timeout

语法:proxy_connect_connect_timeout time
默认值:none
上下文:server
定义与已经代理服务器建立连接的超时时间。

proxy_connect_read_timeout

语法:proxy_connect_read_timeout time
默认值:60s
上下文:server
定义从代理服务器读取响应的超时时间。
超时是在两个连续读操作之间设置的,而不是整个响应的传输。
如果代理服务器在这里时间内没有传输任何内容,则关闭连接。

proxy_connect_write_timeout

语法:proxy_connect_write_timeout time
默认值:60s
上下文:server
设置向代理服务器发送请求的超时时间。
超时设置只在两个连续的写操作之间设置,而不是整个请求的传输。
如果代理服务器在这一次没有收到任何消息,连接将关闭。

proxy_connect_address

语法: proxy_connect_addressaddress [transparent] | off
默认值:none
上下文:server
指定代理服务器的IP地址。 地址可以包含变量。
特殊值等于 none,它使用从连接请求行主机 NAME 解析的IP地址。
注:如果使用 set $<nginx variable> 和 proxy_connect_address $<nginx variable> 同时,你应该使用 proxy_connect_rewrite.patch,参见安装插件以获得更详细的信息。

proxy_connect_bind

语法:proxy_connect_bind address | off
默认值:none
上下文:server
向代理服务器发出传出连接来自具有可选端口的指定本地IP地址。
参数值可以包含变量。 特殊值等于 none,这允许系统自动分配本地IP地址和端口。
透明参数允许与代理服务器的传出连接来源于非本地IP地址,例如,从客户端的实际IP地址:

proxy_connect_bind $remote_addr transparent;

注:如果使用 set $<nginx variable> 和 proxy_connect_bind $<nginx variable> 同时,你应该使用 proxy_connect_rewrite.patch,参见安装插件以获得更详细的信息。

变量:

$connect_host:
连接请求行的主机 NAME。

$connect_port:
连接请求行的端口。

$connect_addr:
远程主机的IP地址和端口,比如"192.168.1.5: 12345"。 IP地址从连接请求行的主机 NAME 解析。

Nginx 兼容性

  • The latest module is compatible with the following versions of nginx:
    • 1.15.8 (mainlain version of 1.15.4+)
    • 1.15.2 (2018/08/02 mainlain version of 1.15.x)
    • 1.14.0 (stable version of 1.14.x)
    • 1.12.1 (stable version of 1.12.x)
    • 1.10.3 (stable version of 1.10.x)
    • 1.8.1 (stable version of 1.8.x)
    • 1.6.3 (stable version of 1.6.x)
    • 1.4.7 (stable version of 1.4.x)

OpenResty Compatibility

The latest module is compatible with the following versions of OpenResty:

  • 1.13.6 (version: 1.13.6.2)
  • 1.15.8 (version: 1.15.8.1rc1)

Tengine Compatibility

This module has been integrated into Tengine 2.3.0.

  • Tengine ngx_http_proxy_connect_module documentation
  • Merged pull request for Tengine 2.3.0.

以上参考了github页说明:http://www.github.com/chobits/ngx_http_proxy_connect_module

猜你喜欢

转载自blog.51cto.com/10316297/2371934
今日推荐