当前位置:   article > 正文

nginx正向代理,支持https模块:ngx_http_proxy_connect_module

http_proxy_connect_module

本文为joshua317原创文章,转载请注明:转载自joshua317博客 nginx正向代理,支持https模块:ngx_http_proxy_connect_module - joshua317的博客

文档地址:https://github.com/chobits/ngx_http_proxy_connect_module#proxy_connect

文档内容

name

This module provides support for the CONNECT method request. This method is mainly used to tunnel SSL requests through proxy servers.

Table of Contents

Example

Configuration Example

  1. server {
  2. listen 3128;
  3. # dns resolver used by forward proxying
  4. resolver 8.8.8.8;
  5. # forward proxy for CONNECT request
  6. proxy_connect;
  7. proxy_connect_allow 443 563;
  8. proxy_connect_connect_timeout 10s;
  9. proxy_connect_read_timeout 10s;
  10. proxy_connect_send_timeout 10s;
  11. # forward proxy for non-CONNECT request
  12. location / {
  13. proxy_pass http://$host;
  14. proxy_set_header Host $host;
  15. }
  16. }

nginx

Copy

Example for curl

With above configuration, you can get any https website via HTTP CONNECT tunnel. A simple test with command curl is as following:

  1. $ curl https://github.com/ -v -x 127.0.0.1:3128
  2. * Trying 127.0.0.1... -.
  3. * Connected to 127.0.0.1 (127.0.0.1) port 3128 (#0) | curl creates TCP connection with nginx (with proxy_connect module).
  4. * Establish HTTP proxy tunnel to github.com:443 -'
  5. > CONNECT github.com:443 HTTP/1.1 -.
  6. > Host: github.com:443 (1) | curl sends CONNECT request to create tunnel.
  7. > User-Agent: curl/7.43.0 |
  8. > Proxy-Connection: Keep-Alive -'
  9. >
  10. < HTTP/1.0 200 Connection Established .- nginx replies 200 that tunnel is established.
  11. < Proxy-agent: nginx (2)| (The client is now being proxied to the remote host. Any data sent
  12. < '- to nginx is now forwarded, unmodified, to the remote host)
  13. * Proxy replied OK to CONNECT request
  14. * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 -.
  15. * Server certificate: github.com |
  16. * Server certificate: DigiCert SHA2 Extended Validation Server CA | curl sends "https://github.com" request via tunnel,
  17. * Server certificate: DigiCert High Assurance EV Root CA | proxy_connect module will proxy data to remote host (github.com).
  18. > GET / HTTP/1.1 |
  19. > Host: github.com (3) |
  20. > User-Agent: curl/7.43.0 |
  21. > Accept: */* -'
  22. >
  23. < HTTP/1.1 200 OK .-
  24. < Date: Fri, 11 Aug 2017 04:13:57 GMT |
  25. < Content-Type: text/html; charset=utf-8 | Any data received from remote host will be sent to client
  26. < Transfer-Encoding: chunked | by proxy_connect module.
  27. < Server: GitHub.com (4)|
  28. < Status: 200 OK |
  29. < Cache-Control: no-cache |
  30. < Vary: X-PJAX |
  31. ... |
  32. ... <other response headers & response body> ... |
  33. ... '-

The sequence diagram of above example is as following:

  1. curl nginx (proxy_connect) github.com
  2. | | |
  3. (1) |-- CONNECT github.com:443 -->| |
  4. | | |
  5. | |----[ TCP connection ]--->|
  6. | | |
  7. (2) |<- HTTP/1.1 200 ---| |
  8. | Connection Established | |
  9. | | |
  10. | |
  11. ========= CONNECT tunnel has been established. ===========
  12. | |
  13. | | |
  14. | | |
  15. | [ SSL stream ] | |
  16. (3) |---[ GET / HTTP/1.1 ]----->| [ SSL stream ] |
  17. | [ Host: github.com ] |---[ GET / HTTP/1.1 ]-->.
  18. | | [ Host: github.com ] |
  19. | | |
  20. | | |
  21. | | |
  22. | | [ SSL stream ] |
  23. | [ SSL stream ] |<--[ HTTP/1.1 200 OK ]---'
  24. (4) |<--[ HTTP/1.1 200 OK ]------| [ < html page > ] |
  25. | [ < html page > ] | |
  26. | | |

Example for browser

You can configure your browser to use this nginx as PROXY server.

  • Google Chrome HTTPS PROXY SETTING: guide & config for how to configure this module working under SSL layer.

Example for Basic Authentication

We can do access control on CONNECT request using nginx auth basic module.
See this guide for more details.

Install

Select patch

  • Select right patch for building:
nginx versionenable REWRITE phasepatch
1.4.x ~ 1.12.xNOproxy_connect.patch
1.4.x ~ 1.12.xYESproxy_connect_rewrite.patch
1.13.x ~ 1.14.xNOproxy_connect_1014.patch
1.13.x ~ 1.14.xYESproxy_connect_rewrite_1014.patch
1.15.2YESproxy_connect_rewrite_1015.patch
1.15.4 ~ 1.16.xYESproxy_connect_rewrite_101504.patch
1.17.x ~ 1.18.0YESproxy_connect_rewrite_1018.patch
1.19.x ~ 1.21.0YESproxy_connect_rewrite_1018.patch
1.21.1YESproxy_connect_rewrite_102101.patch
  • proxy_connect_<VERSION>.patch disables nginx REWRITE phase for CONNECT request by default, which means ifsetrewrite_by_lua and other REWRITE phase directives cannot be used.
  • proxy_connect_rewrite_<VERSION>.patch enables these REWRITE phase directives.

Build nginx

  • Build nginx with this module from source:
  1. $ wget http://nginx.org/download/nginx-1.9.2.tar.gz
  2. $ tar -xzvf nginx-1.9.2.tar.gz
  3. $ cd nginx-1.9.2/
  4. $ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
  5. $ ./configure --add-module=/path/to/ngx_http_proxy_connect_module
  6. $ make && make install

Bash

Copy

Build as a dynamic module

  • Starting from nginx 1.9.11, you can also compile this module as a dynamic module, by using the --add-dynamic-module=PATH option instead of --add-module=PATH on the ./configure command line.
  1. $ $ wget http://nginx.org/download/nginx-1.9.12.tar.gz
  2. $ tar -xzvf nginx-1.9.12.tar.gz
  3. $ cd nginx-1.9.12/
  4. $ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
  5. $ ./configure --add-dynamic-module=/path/to/ngx_http_proxy_connect_module
  6. $ make && make install

Bash

Copy

  • And then you can explicitly load the module in your nginx.conf via the load_module directive, for example,
load_module /path/to/modules/ngx_http_proxy_connect_module.so;
  •  Note that the ngx_http_proxy_connect_module.so file MUST be loaded by nginx binary that is compiled with the .so file at the same time.

Build OpenResty

  • Build OpenResty with this module from source:
  1. $ wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
  2. $ tar -zxvf openresty-1.19.3.1.tar.gz
  3. $ cd openresty-1.19.3.1
  4. $ ./configure --add-module=/path/to/ngx_http_proxy_connect_module
  5. $ patch -d build/nginx-1.19.3/ -p 1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch
  6. $ make && make install

Bash

Copy

Test Suite

  • To run the whole test suite:
  1. $ hg clone http://hg.nginx.org/nginx-tests/
  2. $ export TEST_NGINX_BINARY=/path/to/nginx/binary
  3. $ prove -v -I /path/to/nginx-tests/lib /path/to/ngx_http_proxy_connect_module/t/

Bash

Copy

Error Log

This module logs its own error message beginning with "proxy_connect:" string.
Some typical error logs are shown as following:

  • The proxy_connect module tries to establish tunnel connection with backend server, but the TCP connection timeout occurs.
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"

Directive

proxy_connect

Syntax: proxy_connect
Default: none
Context: server

Enable "CONNECT" HTTP method support.

proxy_connect_allow

Syntax: proxy_connect_allow all | [port ...] | [port-range ...]
Default: 443 563
Context: server

This directive specifies a list of port numbers or ranges to which the proxy CONNECT method may connect.
By default, only the default https port (443) and the default snews port (563) are enabled.
Using this directive will override this default and allow connections to the listed ports only.

The value all will allow all ports to proxy.

The value port will allow specified port to proxy.

The value port-range will allow specified range of port to proxy, for example:

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

proxy_connect_connect_timeout

Syntax: proxy_connect_connect_timeout time
Default: none
Context: server

Defines a timeout for establishing a connection with a proxied server.

proxy_connect_read_timeout

Syntax: proxy_connect_read_timeout time
Default: 60s
Context: server

Defines a timeout for reading a response from the proxied server.
The timeout is set only between two successive read operations, not for the transmission of the whole response.
If the proxied server does not transmit anything within this time, the connection is closed.

proxy_connect_send_timeout

Syntax: proxy_connect_send_timeout time
Default: 60s
Context: server

Sets a timeout for transmitting a request to the proxied server.
The timeout is set only between two successive write operations, not for the transmission of the whole request.
If the proxied server does not receive anything within this time, the connection is closed.

proxy_connect_address

Syntax: proxy_connect_address address | off
Default: none
Context: server

Specifiy an IP address of the proxied server. The address can contain variables.
The special value off is equal to none, which uses the IP address resolved from host name of CONNECT request line.

NOTE: If using set $<nginx variable> and proxy_connect_address $<nginx variable> together, you should use proxy_connect_rewrite.patch instead, see Install for more details.

proxy_connect_bind

Syntax: proxy_connect_bind address [transparent] | off
Default: none
Context: server

Makes outgoing connections to a proxied server originate from the specified local IP address with an optional port.
Parameter value can contain variables. The special value off is equal to none, which allows the system to auto-assign the local IP address and port.

The transparent parameter allows outgoing connections to a proxied server originate from a non-local IP address, for example, from a real IP address of a client:

  1. proxy_connect_bind $remote_addr transparent;

In order for this parameter to work, it is usually necessary to run nginx worker processes with the superuser privileges. On Linux it is not required (1.13.8) as if the transparent parameter is specified, worker processes inherit the CAP_NET_RAW capability from the master process. It is also necessary to configure kernel routing table to intercept network traffic from the proxied server.

NOTE: If using set $<nginx variable> and proxy_connect_bind $<nginx variable> together, you should use proxy_connect_rewrite.patch instead, see Install for more details.

proxy_connect_response

Syntax: proxy_connect_response CONNECT response
Default: HTTP/1.1 200 Connection Established\r\nProxy-agent: nginx\r\n\r\n
Context: server

Set the response of CONNECT request.

Note that it is only used for CONNECT request, it cannot modify the data flow over CONNECT tunnel.

For example:

  1. proxy_connect_response "HTTP/1.1 200 Connection Established\r\nProxy-agent: nginx\r\nX-Proxy-Connected-Addr: $connect_addr\r\n\r\n";

The curl command test case with above config is as following:

  1. $ curl https://github.com -sv -x localhost:3128
  2. * Connected to localhost (127.0.0.1) port 3128 (#0)
  3. * allocate connect buffer!
  4. * Establish HTTP proxy tunnel to github.com:443
  5. > CONNECT github.com:443 HTTP/1.1
  6. > Host: github.com:443
  7. > User-Agent: curl/7.64.1
  8. > Proxy-Connection: Keep-Alive
  9. >
  10. < HTTP/1.1 200 Connection Established --.
  11. < Proxy-agent: nginx | custom CONNECT response
  12. < X-Proxy-Connected-Addr: 13.229.188.59:443 --'
  13. ...

Variables

$connect_host

host name from CONNECT request line.

$connect_port

port from CONNECT request line.

$connect_addr

IP address and port of the remote host, e.g. "192.168.1.5:12345". IP address is resolved from host name of CONNECT request line.

$proxy_connect_connect_timeout

Get or set timeout of proxy_connect_connect_timeout directive.

For example:

  1. # Set default value
  2. proxy_connect_connect_timeout 10s;
  3. proxy_connect_read_timeout 10s;
  4. proxy_connect_send_timeout 10s;
  5. # Overlap default value
  6. if ($host = "test.com") {
  7. set $proxy_connect_connect_timeout "10ms";
  8. set $proxy_connect_read_timeout "10ms";
  9. set $proxy_connect_send_timeout "10ms";
  10. }

nginx

Copy

$proxy_connect_read_timeout

Get or set a timeout of proxy_connect_read_timeout directive.

$proxy_connect_send_timeout

Get or set a timeout of proxy_connect_send_timeout directive.

$proxy_connect_resolve_time

Keeps time spent on name resolving; the time is kept in seconds with millisecond resolution.

  • Value of "" means this module does not work on this request.
  • Value of "-" means name resolving failed.

$proxy_connect_connect_time

Keeps time spent on establishing a connection with the upstream server; the time is kept in seconds with millisecond resolution.

  • Value of "" means this module does not work on this request.
  • Value of "-" means name resolving or connecting failed.

$proxy_connect_response

Get or set the response of CONNECT request.
The default response of CONNECT request is "HTTP/1.1 200 Connection Established\r\nProxy-agent: nginx\r\n\r\n".

Note that it is only used for CONNECT request, it cannot modify the data flow over CONNECT tunnel.

For example:

  1. # modify default Proxy-agent header
  2. set $proxy_connect_response "HTTP/1.1 200\r\nProxy-agent: nginx/1.19\r\n\r\n";

nginx

Copy

The variable value does not support nginx variables. You can use lua-nginx-module to construct string that contains nginx variable. For example:

  1. # The CONNECT response may be "HTTP/1.1 200\r\nProxy-agent: nginx/1.19.6\r\n\r\n"
  2. rewrite_by_lua '
  3. ngx.var.proxy_connect_response =
  4. string.format("HTTP/1.1 200\\r\\nProxy-agent: nginx/%s\\r\\n\\r\\n", ngx.var.nginx_version)
  5. ';

nginx

Copy

Also note that set or rewrite_by_lua* directive is run during the REWRITE phase, which is ahead of dns resolving phase. It cannot get right value of some variables, for example, $connect_addr value is nil. In such case, you should use proxy_connect_response directive instead.

Compatibility

Nginx Compatibility

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

  • 1.19.6 (mainline version of 1.19.x ~ 1.20.x)
  • 1.18.0 (stable version of 1.18.x)
  • 1.16.1 (stable version of 1.16.x)
  • 1.14.2 (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.1)
  • 1.17.8 (version: 1.17.8.2)
  • 1.19.3 (version: 1.19.3.1)

Tengine Compatibility

This module has been integrated into Tengine 2.3.0.

FAQ

See FAQ page.

Known Issues

  • In HTTP/2, the CONNECT method is not supported. It only supports the CONNECT method request in HTTP/1.x and HTTPS.

See Also

Author

LICENSE

See LICENSE for details.

本文为joshua317原创文章,转载请注明:转载自joshua317博客 nginx正向代理,支持https模块:ngx_http_proxy_connect_module - joshua317的博客

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/163741?site
推荐阅读
相关标签
  

闽ICP备14008679号