安装编译工具,编译安装openresty,添加ngx_http_proxy_module 模块。
yum -y install git
yum -y install gcc make
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel
wget https://openresty.org/download/openresty-1.21.4.2.tar.gz
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
tar -zxf openresty-1.21.4.2.tar.gz
cd openresty-1.21.4.2
./configure --prefix=/usr/local/openresty \
--with-debug \
--with-luajit \
--with-threads \
--with-http_v2_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-pcre-jit \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--add-module=../ngx_cache_purge \
--add-module=../ngx_http_proxy_connect_module
patch -d build/nginx-1.21.4/ -p 1 < ..//ngx_http_proxy_connect_module-0.0.7/patch/proxy_connect_rewrite_102101.patch
patching file src/http/ngx_http_core_module.c
Hunk #1 succeeded at 967 (offset 27 lines).
patching file src/http/ngx_http_parse.c
Hunk #2 succeeded at 257 with fuzz 1 (offset 3 lines).
Hunk #3 succeeded at 285 (offset 3 lines).
Hunk #4 succeeded at 301 (offset 3 lines).
patching file src/http/ngx_http_request.c
Hunk #1 succeeded at 1106 (offset 115 lines).
Hunk #2 succeeded at 1744 (offset 127 lines).
patching file src/http/ngx_http_request.h
Hunk #1 succeeded at 43 with fuzz 2 (offset 1 line).
Hunk #2 succeeded at 418 (offset 4 lines).
patching file src/http/ngx_http_variables.c
Hunk #1 succeeded at 163 (offset 2 lines).
make
make install
# sudo /usr/local/openresty/nginx/sbin/nginx -V 2>&1 | sed 's/ --/\n--/g' | egrep --color '.*path.*|$'
nginx version: openresty/1.21.4.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments:
--prefix=/usr/local/openresty/nginx
--with-debug
--with-cc-opt='-DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -O2'
--add-module=../ngx_devel_kit-0.3.2
--add-module=../echo-nginx-module-0.63
--add-module=../xss-nginx-module-0.06
--add-module=../ngx_coolkit-0.2
--add-module=../set-misc-nginx-module-0.33
--add-module=../form-input-nginx-module-0.12
--add-module=../encrypted-session-nginx-module-0.09
--add-module=../srcache-nginx-module-0.33
--add-module=../ngx_lua-0.10.25
--add-module=../ngx_lua_upstream-0.07
--add-module=../headers-more-nginx-module-0.34
--add-module=../array-var-nginx-module-0.06
--add-module=../memc-nginx-module-0.19
--add-module=../redis2-nginx-module-0.15
--add-module=../redis-nginx-module-0.3.9
--add-module=../rds-json-nginx-module-0.16
--add-module=../rds-csv-nginx-module-0.09
--add-module=../ngx_stream_lua-0.0.13
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib
--with-threads
--with-http_v2_module
--with-http_sub_module
--with-http_stub_status_module
--with-http_realip_module
--with-http_gzip_static_module
--with-http_ssl_module
--with-pcre-jit
--add-module=/tmp/openresty/openresty-1.21.4.2/../ngx_cache_purge
--add-module=/tmp/openresty/openresty-1.21.4.2/../ngx_http_proxy_connect_module
--with-stream
--with-stream_ssl_module
--with-stream_ssl_preread_module
这些是 Nginx 编译时的配置选项,用于启用特定的功能模块。
--with-stream
- 启用 Nginx 的 TCP/UDP 代理功能(四层负载均衡),使其可以代理非 HTTP 协议(如数据库、邮件等)
--with-stream_ssl_module
- 为 Stream 模块添加 SSL/TLS 支持,允许加密 TCP/UDP 流量
--with-stream_ssl_preread_module
- 允许在不解密的情况下读取 SSL/TLS 连接的信息(如 SNI),用于基于 TLS 信息的流量路由
--add-module=../ngx_http_proxy_connect_module
- 添加第三方模块,用于支持 CONNECT 方法(常用于 HTTPS 代理)
典型使用场景:
- 构建支持 HTTPS 正向代理的 Nginx
- 实现 TCP/UDP 层的 SSL 加密和路由
- 需要处理非 HTTP 协议代理的情况
这些选项组合后,Nginx 将具备强大的四层代理能力,并能处理加密流量和代理连接。
参考
Module ngx_http_proxy_module
https://nginx.org/en/docs/http/ngx_http_proxy_module.html