江苏省吴江市双板桥路3号兴源网络

0512-63028342

ly@wjxy.net.cn

Nginx 通过stream模块TCP端口映射和负载均衡

查看是否安装了stream模块 用./nginx -V
以上是端口映射配置文件:ngix -c /usr/ngix.con
cat nginx.conf
#user  www www;
#worker_processes auto;
#error_log  /www/wwwlogs/nginx_error.log  crit;
#pid        /www/server/nginx/logs/nginx.pid;
#worker_rlimit_nofile 51200;

#events
#   {
#       use epoll;
#       worker_connections 51200;
#       multi_accept on;
#  }


stream {
        upstream hr9900 {
           hash $remote_addr consistent;
           server 192.168.0.236:9001 weight=5 max_fails=3 fail_timeout=30s;
   }
   upstream hr9018 {
           hash $remote_addr consistent;
           server 192.168.0.236:9018 weight=5 max_fails=3 fail_timeout=30s;
                   }
  server {
       listen 9900;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass hr9900;
            }
  server {
         listen 9901;
         proxy_connect_timeout 1s;                                                                                               proxy_timeout 3s;                                                                                                       proxy_pass hr9018;
         }
}
到/usr/local/nginx/conf/nginx.conf 配置stream模块 其中stream模块与http模块级别相同
stream {

    upstream mysql_3306 {

        server 140.143.11.169:3306 weight=10;

     server 182.61.55.179:3306  weight=10;

    }

    server {

        listen 3307;   # 外部访问我的3007

        proxy_connect_timeout 20s;

        proxy_pass mysql_3306;  # 从这里转发到 upstream

    }

}
backend 表示当其他的不能用的时候我才能用 down   表示不能用 先进入 /usr/local/nginx/sbin 目录 停止 ./nginx -s stop 重启 ./nginx -s reload 启动 ./nginx

发表评论

电子邮件地址不会被公开。 必填项已用*标注