How Nginx and HAProxy works

Hariom Vashisth
5 min readJan 17, 2023

NGINX and HAProxy are both popular open-source software used as reverse proxies and load balancers.

NGINX (pronounced “engine x”) is a web server and reverse proxy that can also be used as a load balancer. It is known for its high performance and stability, and is often used to handle a large number of concurrent connections.

To set up NGINX as a reverse proxy, you would need to configure the server to listen on a specific IP address and port and then use the “proxy_pass” directive in the server block to forward incoming requests to the appropriate backend servers. Additionally, NGINX can be configured to cache the responses from the backend servers to improve performance.

NGINX can also be configured as a load balancer by using the “upstream” and “proxy_pass” directives. The “upstream” directive is used to define a group of backend servers, and the “proxy_pass” directive is used to forward incoming requests to the servers in the group.

HAProxy (High Availability Proxy) is another popular open-source software used as a reverse proxy and load balancer. It is known for its high performance and reliability and is often used to handle a large number of concurrent connections.

To set up HAProxy as a reverse proxy, you would need to configure the server to listen on a specific IP address and port and then use the “use_backend” directive in the frontend block to forward incoming requests to the appropriate backend servers. Additionally, HAProxy can be configured to perform health checks on the backend servers to ensure that only healthy servers receive requests.

HAProxy can also be configured as a load balancer by using the “backend” and “server” directives. The “backend” directive is used to define a group of backend servers, and the “server” directive is used to define the individual servers in the group.

There are many options available to configure both NGINX and HAProxy to suit your specific needs. Some of the common options include:

  • SSL/TLS termination: Both NGINX and HAProxy can be configured to handle SSL/TLS encryption and decryption, allowing for secure communication between the client and the backend servers.
  • Access control: Both NGINX and HAProxy can be configured to restrict access to certain parts of the website based on IP address, user authentication or other criteria.
  • Request routing: Both NGINX and HAProxy can be configured to route requests to different backend servers based on the URL, hostname, or other request attributes.
  • Load balancing algorithm: Both NGINX and HAProxy can be configured to use various load balancing algorithms such as round-robin, least connections, and IP hash to distribute requests among backend servers.

It is important to note that while both of these software are similar in their functionality, they have different configurations, performance characteristics and use cases. It is important to understand the requirements of your use case and choose the best software that fits those needs.

  1. SSL/TLS termination: NGINX: To configure NGINX for SSL/TLS termination, you would need to include the following in your server block:
server {
listen 443 ssl;
ssl_certificate /path/to/ssl_certificate.crt;
ssl_certificate_key /path/to/ssl_certificate.key;
...
location / {
proxy_pass http://backend;
}
}

In this example, NGINX is listening on port 443 and using the SSL certificate and key located at the specified paths. Incoming requests are then forwarded to the backend servers using the proxy_pass directive.

HAProxy: To configure HAProxy for SSL/TLS termination, you would need to include the following in your global section:

global
...
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
ssl-default-bind-options no-sslv3
ssl-default-bind-options no-tlsv10
ssl-default-bind-options no-tlsv11
ssl-default-bind-options no-tls-tickets
...

In this example, HAProxy is configured to use the specified cipher suites, and also to disable SSLv3, TLSv1.0 and TLSv1.1.

2. Access Control: NGINX: To configure access control in NGINX, you can use the “allow” and “deny” directives. For example, to allow access to a location only from a specific IP address or range, you can use the following code in the location block:

location / {
allow 192.168.0.0/16;
deny all;
proxy_pass http://backend;
}

In this example, all requests coming from IP addresses in the range of 192.168.0.0 to 192.168.255.255 will be allowed, and all other requests will be denied.

HAProxy: To configure access control in HAProxy, you can use the “acl” and “http-request” directives. For example, to block all requests coming from a specific IP address, you can use the following code in the frontend block:

acl blocked_ip src 1.2.3.4
http-request deny if blocked_ip

In this example, all requests coming from IP address 1.2.3.4 will be blocked.

3. Request Routing: NGINX: To route requests to different backend servers based on the URL or hostname, you can use the “location” and “proxy_pass” directives. For example, to route requests for a specific URL to one backend server, and all other requests to another backend server, you can use the following code in the

server block:

server {
...
location /special {
proxy_pass http://backend1;
}
location / {
proxy_pass http://backend2;
}
}

In this example, requests for the URL “/special” will be forwarded to backend server 1, and all other requests will be forwarded to backend server 2.

HAProxy: To route requests to different backend servers based on the URL or hostname, you can use the “acl” and “use_backend” directives. For example, to route requests for a specific hostname to one backend server, and all other requests to another backend server, you can use the following code in the frontend block:

acl special_host hdr(host) -i special.example.com
use_backend special_backend if special_host
default_backend default_backend

In this example, requests with the hostname “special.example.com” will be forwarded to the “special_backend” backend server, and all other requests will be forwarded to the “default_backend” server.

4. Load balancing algorithm: NGINX: To configure the load balancing algorithm in NGINX, you can use the “upstream” and “proxy_pass” directives. For example, to use the round-robin algorithm, you can use the following code in the server block:

http {
...
upstream backend {
server backend1;
server backend2;
...
server backendN;
}
...
server {
...
location / {
proxy_pass http://backend;
}
}
}

In this example, requests will be distributed among the backend servers defined in the “upstream” block using the round-robin algorithm.

HAProxy: To configure the load balancing algorithm in HAProxy, you can use the “balance” directive in the backend block. For example, to use the round-robin algorithm, you can use the following code in the backend block:

backend backend
balance roundrobin
server backend1 backend1:80 check
server backend2 backend2:80 check
...
server backendN backendN:80 check

In this example, requests will be distributed among the backend servers defined in the backend block using the round-robin algorithm.

It is important to note that these are just examples of how these options can be configured and there may be variations depending on your specific use case. Additionally, both NGINX and HAProxy have many other options available for advanced configuration and it is recommended to refer to the official documentation for more information.

--

--