A forward proxy module for CONNECT request handling
A forward proxy module for CONNECT request handling
This module provides support for the CONNECT method request. This method is mainly used to tunnel SSL requests through proxy servers.
server {
listen 3128;
# dns resolver used by forward proxying
resolver 8.8.8.8;
# forward proxy for CONNECT requests
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_data_timeout 10s;
# defined by yourself for non-CONNECT requests
# Example: reverse proxy for non-CONNECT requests
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
}
}resolver directive MUST be configured globally in server {} block (or http {} block).location {} block, upstream {} block and any other standard backend/upstream directives, such as proxy_pass, do not impact the functionality of this module. (The proxy_connect module only executes the logic for requests that use the CONNECT method and that have a data flow under this tunnel.)location {} block as following:location / {
return 403 "Non-CONNECT requests are forbidden";
}With above configuration(configuration example
), you can get any https website via HTTP CONNECT tunnel. A simple test with command curl is as following:
…The sequence diagram of above example is as following:
……With above configuration(configuration example for CONNECT request in https), you can get any https website via HTTPS CONNECT tunnel(CONNECT request in https). A simple test with command curl is as following:
Tips on using curl command:
-x https://... makes curl send CONNECT request in https.--proxy-insecure disables ssl signature verification for ssl connection established with nginx proxy_connect server(https://localhost:3128), but it does not disable verification with proxied backend server(https://nginx.org in the example below).-k option.…You can configure your browser to use this nginx as PROXY server.
We can do access control on CONNECT request using nginx auth basic module.
See this guide for more details.
patch/ directory of this module. You dont need to download the patch directly from web page.| nginx version | enable REWRITE phase | patch |
|---|---|---|
| 1.4.x ~ 1.12.x | NO | proxy_connect.patch |
| 1.4.x ~ 1.12.x | YES | proxy_connect_rewrite.patch |
| 1.13.x ~ 1.14.x | NO | proxy_connect_1014.patch |
| 1.13.x ~ 1.14.x | YES | proxy_connect_rewrite_1014.patch |
| 1.15.2 | YES | proxy_connect_rewrite_1015.patch |
| 1.15.4 ~ 1.16.x | YES | proxy_connect_rewrite_101504.patch |
| 1.17.x ~ 1.18.x | YES | proxy_connect_rewrite_1018.patch |
| 1.19.x ~ 1.21.0 | YES | proxy_connect_rewrite_1018.patch |
| 1.21.1 ~ 1.22.x | YES | proxy_connect_rewrite_102101.patch |
| 1.23.x ~ 1.24.0 | YES | proxy_connect_rewrite_102101.patch |
| 1.25.0 ~ 1.26.x | YES | proxy_connect_rewrite_102101.patch |
| 1.27.1 | YES | proxy_connect_rewrite_102101.patch |
| OpenResty version | enable REWRITE phase | patch |
|---|---|---|
| 1.13.6 | NO | proxy_connect_1014.patch |
| 1.13.6 | YES | proxy_connect_rewrite_1014.patch |
| 1.15.8 | YES | proxy_connect_rewrite_101504.patch |
| 1.17.8 | YES | proxy_connect_rewrite_1018.patch |
| 1.19.3 | YES | proxy_connect_rewrite_1018.patch |
| 1.21.4 | YES | proxy_connect_rewrite_102101.patch |
| 1.25.3 | YES | proxy_connect_rewrite_102101.patch |
proxy_connect_<VERSION>.patch disables nginx REWRITE phase for CONNECT request by default, which means if, set, rewrite_by_lua and other REWRITE phase directives cannot be used.proxy_connect_rewrite_<VERSION>.patch enables these REWRITE phase directives.$ wget http://nginx.org/download/nginx-1.9.2.tar.gz
$ tar -xzvf nginx-1.9.2.tar.gz
$ cd nginx-1.9.2/
$ 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--add-dynamic-module=PATH option instead of --add-module=PATH on the ./configure command line.$ wget http://nginx.org/download/nginx-1.9.12.tar.gz
$ tar -xzvf nginx-1.9.12.tar.gz
$ cd nginx-1.9.12/
$ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
$ ./configure --add-dynamic-module=/path/to/ngx_http_proxy_connect_module
$ make && make installload_module directive, for example,load_module /path/to/modules/ngx_http_proxy_connect_module.so;$ wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
$ tar -zxvf openresty-1.19.3.1.tar.gz
$ cd openresty-1.19.3.1
$ ./configure --add-module=/path/to/ngx_http_proxy_connect_module
$ patch -d build/nginx-1.19.3/ -p 1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch
$ make && make install$ hg clone http://hg.nginx.org/nginx-tests/
# If you use latest lua-nginx-module that needs lua-resty-core and
# lua-resty-lrucache, you should add "lua_package_path ...;" directive
# into nginx.conf of test cases. You can use the following command:
#
# $ export TEST_NGINX_GLOBALS_HTTP='lua_package_path "/path/to/nginx/lib/lua/?.lua;;";'
$ export TEST_NGINX_BINARY=/path/to/nginx/binary
$ prove -v -I /path/to/nginx-tests/lib /path/to/ngx_http_proxy_connect_module/t/This module logs its own error message beginning with "proxy_connect:" string.
Some typical error logs are shown as following:
2019/08/07 17:27:20 [error] 19257#0: *1 proxy_connect: upstream connect timed out (peer:216.58.200.4:443) while connecting to upstream, client: 127.0.0.1, server: , request: "CONNECT www.google.com:443 HTTP/1.1", host: "www.google.com:443"Syntax: *proxy_connect
No open issues yet, or sync has not completed.