Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can nginx do TCP load balance with SSL termination

Tags:

nginx

tcp

load

Due to some reason, I need to set up Nginx TCP load balance, but with SSL termination. I am not sure whether Nginx can do this. Since TCP is layer 4, SSL is layer 5, SSL pass-thru definitely work. But with SSL-termination?

like image 850
adiggo Avatar asked Sep 09 '16 22:09

adiggo


People also ask

Does NGINX support TCP load balancing?

Load balancing refers to efficiently distributing network traffic across multiple backend servers. In NGINX Plus Release 5 and later, NGINX Plus can proxy and load balance Transmission Control Protocol) (TCP) traffic.

Does NGINX do SSL termination?

Terminate HTTPS traffic from clients, relieving your upstream web and application servers of the computational load of SSL/TLS encryption.

Should SSL be terminated at load balancer?

SSL termination at load balancer is desired because decryption is resource and CPU intensive. Putting the decryption burden on the load balancer enables the server to spend processing power on application tasks, which helps improve performance. It also simplifies the management of SSL certificates.

Is load balancing possible with NGINX?

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.


1 Answers

Nginx can act as L3/4 balancer with stream module: https://www.nginx.com/resources/admin-guide/tcp-load-balancing/

Because SSL still tcp - Nginx can proxy SSL traffic without termination.

Also stream module can terminate SSL traffic, but it's optional.

Example 1: TCP tunnel for IMAP over SSL without SSL termination

stream {
    upstream stream_backend {
        server backend1.example.com:993;
        server backend2.example.com:993;
    }
    server {
        listen 993;
        proxy_pass stream_backend;
    }
}

In this case, SSL termination processed by backend1/2.

Example 2: TCP tunnel for IMAP with SSL termination.

stream {
    upstream stream_backend {
        server backend1.example.com:443;
        server backend2.example.com:443;
    }
    server {
        listen 993 ssl;
        proxy_pass stream_backend;
        ssl_certificate        /etc/ssl/certs/server.crt;
        ssl_certificate_key    /etc/ssl/certs/server.key;
    }
}

In this case traffic between nginx and backend1/2 unencrypted (IMAP 443 port used).

Example 3: Receive unencrypted and encrypt it

stream {
    upstream stream_backend {
        server backend1.example.com:993;
        server backend2.example.com:993;
    }
    server {
        listen 443;
        proxy_pass stream_backend;
        proxy_ssl  on;
        proxy_ssl_certificate     /etc/ssl/certs/backend.crt;
        proxy_ssl_certificate_key /etc/ssl/certs/backend.key;
    }
}

So, clients connect to our nginx without SSL and this traffic proxed to backend1/2 using SSL encryption.

like image 178
Dmitry MiksIr Avatar answered Sep 20 '22 02:09

Dmitry MiksIr