nginx四层tcp代理刨析


概述: “The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.” 来自官方的说明,nginx从1.9.0开始支持了tcp模式的代理,我们只需要在编译的时候加上 --with-stream 参数即可,而该参数默认是关闭的。下面在centos7下演示,实现tcp 443端口代理http非80端口的ssl配置。

1、首先我们需要增加系统对正则和ssl支持的模块,如下:
#yum install -y openssl-devel pcre-devel

2、例如这样编译安装:
#cd /usr/local/src/
#tar -zxvf nginx-1.18.0.tar.gz
#useradd nginx
#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream --with-http_v2_module --with-http_sub_module --with-http_gunzip_module --with-http_realip_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
#cd /usr/local/nginxe/sbin/
# ./nginx -V
nginx version: nginx/1.18.0
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/nginx --user=nginx --group=nginx --with-stream --with-http_v2_module --with-http_sub_module --with-http_gunzip_module --with-http_realip_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module

3、tcp代理的配置与使用 ,我们需要注意:stream与http段是同一级别的指令(同一级别?哈哈个人定义这样的说法),所以不能相互包含,而是独立

http {
 ......
}

stream {
upstream tcp_proxy0102{
        least_conn;
        server 172.1.1.245:8888 weight=5;
        server 172.1.1.246:8888 weight=5;
}

server {
listen 80 so_keepalive=on;
listen 443;
allow 47.0.0.0/8;
allow 8.0.0.0/8;
allow 172.16.0.0/16;
allow 1.5.2.28/32;
deny all;
proxy_connect_timeout 5s;
proxy_timeout 5s;
proxy_pass tcp_proxy0102;
}
}

4、server段非80端口ssl的配置

server {
        listen       8888 ssl;
        server_name  1.xinyuer.cn;
        error_log /var/log/nginx/1.xinyuer.cn-er.log;
        access_log /var/log/nginx/1.xinyuer.cn-ac.log;

        ssl_certificate /usr/local/nginx/ssl/1.pem;
        ssl_certificate_key     /usr/local/nginx/ssl/1.key;
        ssl_prefer_server_ciphers on;
        ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES128-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA128:DHE-RSA-AES128-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA128:ECDHE-RSA-AES128-SHA384:ECDHE-RSA-AES128-SHA128:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA128:DHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA384:AES128-GCM-SHA128:AES128-SHA128:AES128-SHA128:AES128-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
        ssl_ecdh_curve secp384r1;
        proxy_ssl_server_name on;
        location / {
           proxy_pass  https://tech.xinyuer.cn;
           proxy_set_header  Host $host;
           proxy_set_header  X-Real-IP  $remote_addr;
           proxy_set_header  X-Forwarded-For $http_x_forwarded_for;
        }
}







本文标签: nginx 四层 tcp代理

内容版权声明:【蓝色网居】部分资源来源于网络,如有侵犯您的所有权,请随时告知我们,我们将立即删除!感谢配合!

转载请注明出处:https://blog.ff56.cn/web/1657872765.html


【手机扫一扫查看文本】

手机扫一扫 手机扫一扫查看文本 手机扫一扫





语言选择 重写 案例分析 try_files 强匹配 404 自定义403 参数重写 $http_host 编译安装 跳转rewrite 域名 自启动 跳转 访问控制 错误日志 防IP恶意 防IP ip白名单 cookie geoip pass_proxy的使 nginx增加 定义404跳转 白名单 模糊匹配 nginx插件 或(or) nginx 错误页