nginx做TCP代理

要实现TCP代理,可以使用Nginx的stream模块。stream模块允许Nginx作为一个转发代理来处理TCP流量,包括TCP代理、负载均衡和SSL终止等功能。

以下是配置Nginx实现TCP代理的基本步骤:

  1. 在Nginx配置文件中添加stream块,并在该块中配置代理服务:
nginx 复制代码
stream {
    server {
        listen 12345;  # 设置监听端口
        proxy_pass destination_server:destination_port;  # 配置代理目标服务器和端口
    }
}
  1. 将上述配置添加到Nginx配置文件中的http块之后。确保在启动或重新加载Nginx时加载此配置。

  2. 重新加载Nginx以使配置生效:

bash 复制代码
sudo nginx -s reload

通过上述配置,Nginx将会将收到的TCP连接传递给目标服务器和端口,并将其响应返回给客户端,实现了TCP代理的功能。

确认模块是否存在

要确定Nginx是否安装了TCP代理模块,您可以执行以下步骤:

  1. 查看Nginx的详细版本信息:
bash 复制代码
nginx -V
  1. 在输出中查找--with-stream参数,如果存在该参数,则表示Nginx已安装了stream模块,支持TCP代理功能。

  2. 如果--with-stream参数不存在,表示Nginx未编译安装stream模块,无法支持TCP代理功能。

如果您需要启用TCP代理功能,可以重新编译并安装Nginx,并在编译时包含--with-stream参数。然后按照先前提供的步骤配置Nginx以实现TCP代理功能。

完整配置文件

本例做了一个MySQL的TCP代理,使得部署在内网的MySQL可以通过nginx的公网IP访问。

bash 复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


}
stream {
    server {
        listen 3306;  # 设置监听端口
        proxy_pass 内网IP:3306;  # 配置代理目标服务器和端口
    }
}
相关推荐
10WTW0112 小时前
微机原理习题
网络
行走__Wz13 小时前
【网工入门-eNSP模拟-01】ip地址配置
网络·tcp/ip·智能路由器
yyuuuzz13 小时前
独立站运营的几个技术层面常见问题
大数据·运维·服务器·网络·数据库·aws
utf8mb4安全女神13 小时前
MySQL8.0.43的下载安装【二进制安装】【shell脚本】【环境准备】【my.cnf配置】【修改密码】
linux·服务器·网络
yang)13 小时前
jesd204B应用笔记
网络·笔记
葡萄皮sandy13 小时前
SSE和WebSocket
网络·websocket·网络协议
酉鬼女又兒13 小时前
零基础入门计算机网络网际层核心:IP数据报发送与转发完整流程、静态路由配置方法、路由环路成因与解决方案及历年考研经典例题深度解析
网络·tcp/ip·计算机网络·考研·职场和发展
qq_1958216514 小时前
6. 应用层协议实现:CoE协议栈集成、对象字典配置、PDO映射
java·服务器·网络
程序猿零零漆14 小时前
Python核心进阶三连:闭包装饰器、深浅拷贝、网络编程从原理到实战
网络·python
袖手蹲14 小时前
K10 百炼 AI 语音助手从网络配置到全链路语音交互的嵌入式实战
网络·人工智能·交互