[关闭]
@orangleliu 2016-09-22T04:29:55.000000Z 字数 3080 阅读 11994

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

nginx


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

nginx streaming 文档地址
nginx streaming ssl 配置

软件环境

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. }

测试

  1. telnet 127.0.0.1 30003

第一次会代理到 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. }

代理SS (shadowsocks)

测试失败

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

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

配置过程

错误如下,可以推断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 安装,配置都比较简单,非常好。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注