0

I am trying to set up load balancing for an application that utilizes multiple ports. The application's initial connection being made on TCP port "A" and a secondary connection being made on TCP port "B".

Note - The application is not browser based.

How can I guarantee if first connection goes to Node X then the secondary connection will also send to the Node X, and not Node Y?

Here's what I want:

USER1 --> Nginx --> Node X:A
USER1 --> Nginx --> Node X:B

Here's what I don't want since it will break the app:

USER1 --> Nginx --> Node X:A
USER1 --> Nginx --> Node Y:B

Does anyone have any suggestions/recommendations on how to approach this configuration in Nginx? Remember, the application is not browser based so standard browser-centric approaches using cookies, profiles, etc., don't seem to apply - at least none that I've found.

Any assistance would be greatly appreciated.

Thanks

0

Here is a very basic and simple config. The key here is hash $remote_addr consistent;. What is does is, it creates mapping of client IP and backend server and makes sure that each time request from specific IP is sent to its mapped backend unless it is down.
You are free to add additional config line keepalive, timeout etc.

stream {    
#  Custom log format to capture source and dest IP and request status
   log_format tcp_proxy '[$time_local] $remote_addr $protocol $upstream_addr'
                        '$status $bytes_sent $bytes_received $session_time';

   upstream tcp_backend {
      hash $remote_addr consistent;
      server backend1:ip;
      server backend2:ip;
   }

   server {
      listen ip:port;
      proxy_pass tcp_backend;
      access_log  /var/log/nginx-tcp_server-access.log   tcp_proxy;
   }
}

You can find additional config in below URL
https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
http://nginx.org/en/docs/http/ngx_http_upstream_module.html

4
  • Thanks for your answer. Is that mean user will have the same server all time? Is there any way to reset like after 10 minutes? – Jibon Jun 25 '18 at 7:11
  • No, that won't be possible if you use $remote_addr as a key, there are lots of other keys like $request_id, $remote_port(nginx.org/en/docs/varindex.html), but I don't know how your application communicates over TCP or how sessions are managed. By the way, the feature of proxy that you are looking for is 'stickiness', unfortunately is part of Nginx Plus(paid version of Nginx). – Tejas Sarade Jun 25 '18 at 7:57
  • Thanks again for reply. Is it possible with HAproxy? If user use the same server then it won't be what I wanted to make load balancing. I am open to move HAproxy if that can solve my problem. Any reference will be greatly appreciate. – Jibon Jun 26 '18 at 7:05
  • Yes, in your case HAProxy seems better choice. It has the option to set expiry for hash table. Exact scenario you are looking is explained here link. Though I don't have the sample configuration handy with me, you can find example of TCP HAProxy from documentation and add the stickiness specific config to it. – Tejas Sarade Jun 26 '18 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.