nginx和haproxy的四层端口转发

nginx的端口转发

1.yum 安装方式
vim /etc/nginx/conf.d/proxy.conf
stream {
  upstream proxy_mysql {
  server 192.168.1.10:3306;
  }
  
  server {
  listen 9999;
  proxy_pass proxy_mysql;
  }
}
[root@localhost ~]# nginx -t
nginx: [emerg] unknown directive "stream" in /etc/nginx/conf.d/proxy.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
#若是出现上面的错误,执行下面的操作后再nginx -t

[root@localhost ~]# yum -y install nginx-all-modules.noarch
[root@localhost ~]# nginx -t
nginx: [emerg] "stream" directive is not allowed here in /etc/nginx/conf.d/proxy.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
#安装完模块之后继续问题,这个问题表示stream指令不允许防止在这里,那当前的位置是在http指令中。所以把他移出来。
vim /etc/nginx.conf
events {
  ......
}

http {
  ......
}
#配置方法1 
stream {
  server {
  listen 9999;
  proxy_pass 192.168.1.10:3306;
  }
}
#配置方法2
stream {
  upstream proxy_mysql {
    server 192.168.1.10:3306
  }
  server {
    listen 9999;
	proxy_ass proxy_mysql;
  }
}

# 然后再nginx -t 即可通过。再nginx -s reload即可,不行就systemctl restart nginx
2.编译安装方式的端口转发
nginx -V  #获取原来的编译参数
./configure --with-stream (添加上即可)

Haproxy的四层转发

vim /etc/haproxy.haproxy.cfg
listen proxy_mysql(自定义名)
bind 0.0.0.0:9998
mode tcp
server server_name(自定义名) 127.0.0.1:3306 check inter 2000 fall 3 rise 5

重启haproxy即可.
posted @ 2021-09-10 18:24  少林寺驻峨眉山大神父  阅读(434)  评论(0)    收藏  举报