【Nginx系列】---Nginx配置tcp转发

参考 Nginx 配置文件:

nginx 复制代码
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

stream {
    # 第一个服务转发
    upstream mysqltest {
        server 172.16.187.142:9000;
    }

    server {
        listen 9000;
        proxy_pass mysqltest;
    }

    # 第二个服务转发
    server {
        listen 8018;                        # 本地监听 8018 端口
        proxy_pass 172.16.187.130:8018;     # 转发到目标服务
    }
}

确保在配置中只有 一个 stream,否则会导致 Nginx 配置解析失败。

测试流程

完成上述修改后,按照以下步骤验证:

  1. 检查 Nginx 配置是否正确:

    bash 复制代码
    nginx -t
  2. 重启 Nginx 服务:

    bash 复制代码
    systemctl restart nginx
  3. 测试本地端口是否正常监听:

    bash 复制代码
    netstat -tulnp | grep 8018
  4. 测试通过 Nginx 转发是否正常:

    bash 复制代码
    telnet 127.0.0.1 8018
相关推荐
m0_748251351 小时前
Mac安装配置使用nginx的一系列问题
运维·nginx·macos
web150854159351 小时前
Nginx 配置前端后端服务
运维·前端·nginx
A星空1232 小时前
Linux mount命令
linux·运维·服务器
若云止水3 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_str_rbtree_insert_value
linux·nginx·ubuntu
丁卯4043 小时前
Linux(centOS) 命令提示符格式修改(PS1)
linux·运维·服务器·centos
Dracen_Wu3 小时前
修改CentOS 8 yum源
linux·运维·centos
lqlj22334 小时前
Linux常用命令
linux·运维·服务器
wanhengidc4 小时前
怎样分辨是否是高防服务器?
运维·服务器·网络
gma9994 小时前
【GB28181】 SIP信令服务器
运维·服务器
张烫麻辣亮。5 小时前
【教程】使用docker+Dify搭建一个本地知识库
运维·docker·容器