Nginx折腾-TCP代理和负载均衡

by Jiawei zhang, at 15 June 2020, tags : 点击纠错 点击删除
使用CN2/CN2GIA顶级线路,支持Shadowsocks/V2ray科学上网,支持支付宝付款,每月仅需 5 美元
## 加入品葱精选 Telegram Channel ##

@orangleliu 2016-09-22T04:29:55.000000Z 字数 3080 阅读 9206

nginx

Nginx1.9 版本以后增加 stream模块,可以对tcp,udp请求进行代理和负载均衡了,今天来感受一下。

nginx streaming 文档地址
nginx streaming ssl 配置

  • Centos 6.7
  • openresty/1.9.15.1 (config时候增加选项 --with-stream --with-stream_ssl_module )

nginx对应版本1.9.3+以上,编译安装对于高版本nginx基本就是三板斧,所以掠过。

都在一个机器上, nginx监听30003端口,然后开两个窗口,用nc监听 7773,7774端口。
nginx配置

  1. / nc -l 7773
  2. telnet--> nginx(30003) -->
  3. \ nc -l 7774

配置

  1. stream {
  2. upstream backend {
  3. server 127.0.0.1:7773;
  4. server 127.0.0.1:7774;
  5. }
  6. server {
  7. listen 127.0.0.1:30003;
  8. proxy_timeout 20s;
  9. proxy_pass backend;
  10. }
  11. }

测试

第一次会代理到 7773端口,第二次会到7774端口,挺好用。

下面是一个ssl 然后ip hash方式的负载均衡(tcp也支持几种负载均衡方式 round-robin, least_conn least_time, hash等)

  1. stream {

  2. upstream backend {

  3. hash $remote_addr;

  4. server 192.168.59.3:9344;

  5. server 192.168.59.3:9345;

  6. }

  7. server {

  8. listen 9344 ssl;

  9. ssl_certificate /data/keys/CAcert.pem;

  10. ssl_certificate_key /data/keys/privkey.pem;

  11. ssl_session_cache shared:SSL:10m;

  12. ssl_session_timeout 2h;

  13. proxy_timeout 20s;

  14. proxy_pass backend;

  15. }

  16. }

测试失败

测试的场景:测试的主机是一台国外的VPS,代号host1。另外还有两个其他人的SS代理,用户名密码,以及加密方式都相同。 然后用host1代理host2, host3的ss服务,负载均衡使用轮训策略。 修改ss本地客户端配置,浏览器访问墙外的网站。

  1. / host2(ss)
  2. client --> host1(nginx)
  3. \ host3(ss)
  • host1 63.221.100.34

  • host2 45.76.39.146

  • host3 23.83.123.88

    1. host2, host3 ss已经启动,端口都是 9999

    2. host1 配置nginx, stream部分, 和 http块同级

      1. http {...}

      2. stream {

      3. upstream backend {

      4. server 45.76.39.146:9999;

      5. server 23.83.123.88:9999;

      6. }

      7. server {

      8. listen 63.221.100.34:9000;

      9. proxy_timeout 20s;

      10. proxy_pass backend;

      11. }

      12. }

      配置完成重启nginx

    3. 本地ss 客户端指向host1的地址和端口

    4. 测试结果 无法正常访问google (从nginx errorlog中能看到,upstream tcp 已经起作用了, 可以代理到host2, host3, 就是没有成功)

错误如下,可以推断nginx能代理 TCP 请求,可是应用无法正常响应,怀疑proxy 方式来访问ss不好用。通过在host
1上抓包发现,host1可以往host2, host3发送请求。后来想一想nginx并不能完整的区分client的一个http请求,如果host2, host3只拿到http请求的部分tcp包,那么也无法正常代理(负载均衡的思路可以去掉了,后来想了下应该可以用ip hash来解决这个事)

  1. 2016/08/23 23:25:25 [error] 27384#27384: *266 connect() failed (111: Connection refused) while connecting to upstream, client: 182.18.73.162, server: 63.221.100.34:9000, upstream: "45.76.39.146:9999", bytes from/to client:0/0, bytes from/to upstream:0/0
  2. 2016/08/23 23:26:15 [notice] 27388#27388: signal process started
  3. 2016/08/23 23:28:33 [error] 27389#27389: *768 recv() failed (104: Connection reset by peer) while proxying connection, client: 182.18.73.162, server: 63.221.100.34:9000, upstream: "23.83.123.88:9999", bytes from/to client:239/0, bytes from/to upstream:0/239

现在不做负载均衡,只是配置一个backend来做tcp代理,于是注释掉host3.

  1. upstream backend {
  2. server 45.76.39.146:9999;
  3. #server 23.83.123.88:9999; # 后来掉一个backend
  4. }

在进行测试, 这次 打开host2上ss代理的 -v 模式

  1. root@fendou liuzhizhi]# ss-server -c /etc/shadowsocks.json -v
  2. 2016-08-24 02:18:39 INFO: initializing ciphers... aes-256-cfb
  3. 2016-08-24 02:18:39 INFO: port reuse enabled
  4. 2016-08-24 02:18:39 INFO: listening at 45.76.39.146:9999
  5. 2016-08-24 02:18:40 INFO: accept a connection
  6. 2016-08-24 02:18:40 INFO: connect to [7759:5fcd:7e17:aaef:2602:7373:56ec:d45e]:58426
  7. 2016-08-24 02:18:40 INFO: accept a connection
  8. 2016-08-24 02:18:40 ERROR: authentication error from 63.221.100.34
  9. 2016-08-24 02:18:40 INFO: current server connection: 1
  10. 2016-08-24 02:18:41 INFO: accept a connection
  11. 2016-08-24 02:18:41 ERROR: authentication error from 63.221.100.34
  12. 2016-08-24 02:18:41 INFO: current server connection: 1
  13. 2016-08-24 02:18:41 INFO: accept a connection
  14. 2016-08-24 02:18:41 ERROR: authentication error from 63.221.100.34

浏览器测试还是不能正常代理,页面都无法打开。(具体原因还需查查)

nginx tcp proxy 安装,配置都比较简单,非常好。

最简单好用的 VPS,没有之一,注册立得 100 美金
comments powered by Disqus

See Also