Table of Contents
- Nginx web server
- Features of Nginx web server
- Usages of Nginx web server with load balancing
- Installation of Nginx web server with load balancer and also proxy setup.
Nginx web server
Nginx web server is a versatile, open-source web server software and this is helpful to configuration of web application during the deployment because of this feature, it has gained popularity for its high performance and low resource usage. Nginx also handle the load balancing as well as proxy setting on configuration.
Nginx is very good web server to use for web application and for both load balancing and proxying is a common use case in nginx. This can be helpful to us distribute incoming traffic across multiple servers while also acting as a reverse proxy.
Features of Nginx web server
- Web Server: Nginx can serve static content such as HTML, CSS, and images efficiently.
- Reverse Proxy: It can act as an intermediary for requests from clients seeking resources from other servers.
- Load Balancer: Nginx can distribute incoming network traffic across multiple servers to ensure no single server becomes overwhelmed.
- HTTP Cache: It can cache responses to improve performance and reduce load on backend servers.
- Mail Proxy: Nginx can also be used to proxy email traffic.
Usages of Nginx web server with load balancing
Nginx can be configured to serve dynamic content using FastCGI, SCGI handlers, or WSGI application servers. It also supports TLS/SSL with SNI and OCSP stapling, making it a robust choice for secure web applications
Installation of Nginx web server with load balancer and also proxy setup.
- Install Nginx
First, We need to ensure that Nginx is installed on your system. You can install it using your package manager:
For Debian-based systems (like Ubuntu):
sudo apt update
sudo apt install nginx
For Red Hat-based systems (like CentOS):
sudo yum install nginx
For Fedora:
sudo dnf install nginx - Setup the Nginx web server for Load Balancing and Proxying for secure connection
You need to edit the Nginx configuration file, typically located at ‘/etc/nginx/nginx.conf’, or create a new configuration file in ‘/etc/nginx/conf.d/’ or ‘/etc/nginx/sites-available/’.
Here’s a basic example of an Nginx configuration that sets up load balancing and acts as a reverse proxy:
#Load balancing configuration
http {
upstream myapp {
server app1.example.com;
server app2.example.com;
server app3.example.com;# Optional: define load balancing method
# Least connections:
# least_conn;
# Weighted load balancing:
# server app1.example.com weight=3;
# server app2.example.com weight=1;
}
server
{
listen 80;
server_name www.example.com;
location /
{
proxy_pass http://myapp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Optional: additional proxy settings
# proxy_redirect off;
# proxy_buffering off;
}
}
} - Explanation of Key Directives during Nginx load balancing setup
upstream myapp : Defines a group of backend servers for load balancing. You can include as many ‘server’ directives as needed. You can also specify weights for weighted load balancing or use the ‘least_conn’ directive for least connections load balancing.
proxy_pass http://myapp: Forwards requests to the upstream group named ‘myapp’.
proxy_set_header: Passes headers to the backend servers, which is useful for preserving the original client IP and the protocol used. - Need to Restart the Nginx afte setup the load balancing in Nginx as well as proxy
After making changes to the configuration file, you need to reload or restart Nginx to apply the changes:
sudo systemctl reload nginx
OR
sudo systemctl restart nginx - Now time to Test the application after setup and restart the nginx web server
You can test your configuration for syntax errors before reloading:
sudo nginx -t - Additional Considerations
Health Checks: Nginx does not support active health checks out of the box. Because of this, you might be consider using the Nginx web server plus commercial version or a third-party module.
SSL/TLS: For secure connections, you will need to set up SSL/TLS certificate for secure connections. This involves additional configuration for certificates and handling HTTPS requests.
Caching: You might want to configure caching for better performance.
This setup provides a basic load balancing and proxying configuration. Depending on your specific needs, you might need to adjust or add more directives.