Configuring Nginx as a Load Balancer on CentOS
Nginx can distribute HTTP requests across several application servers, improving availability and making planned maintenance easier. On CentOS, the basic design places Nginx on a public-facing host while backend services listen on private addresses such as 10.0.2.11 and 10.0.2.12.
This arrangement suits Australian teams running workloads in Sydney, Melbourne, Brisbane or Perth data centres. It can also support applications serving customers through the NBN, although bandwidth and latency should be measured between the proxy and each backend rather than assumed from the public internet connection.
Before changing a production server, verify the CentOS release and repository policy. CentOS 7 is end-of-life, while CentOS Stream has a rolling update model; many organisations now choose Rocky Linux or AlmaLinux for longer-term compatibility. General Linux administration references, such as this Linux and Unix category, can help with related service-management tasks.
Prepare The CentOS Hosts
Use one machine as the reverse proxy and at least two separate backend hosts. Give each server a stable address, either through static network configuration or DHCP reservations. In a small Melbourne office lab, for example, the proxy might use 10.10.10.10, with application servers at 10.10.10.11 and 10.10.10.12.
Update packages and install Nginx using the package manager appropriate to the release:
sudo dnf update -y
sudo dnf install -y nginx
On older CentOS 7 systems, yum may be the available command. If the standard repositories do not provide the required Nginx version, enable a trusted repository rather than downloading an unverified binary.
Enable And Inspect Nginx
Start the service and configure it to launch after a reboot:
sudo systemctl enable --now nginx
sudo systemctl status nginx
Check the installed version and validate the default configuration before making changes:
nginx -v
sudo nginx -t
CentOS commonly runs SELinux in enforcing mode. Keeping it enabled is preferable, but Nginx may need permission to connect to upstream services:
sudo setsebool -P httpd_can_network_connect 1
This setting allows the web process to make network connections while leaving the broader security policy active.
Define The Upstream Pool
Create a site configuration such as /etc/nginx/conf.d/app.conf. The upstream block names the backend pool and defines how requests are shared:
upstream application_pool {
least_conn;
server 10.10.10.11:8080 max_fails=3 fail_timeout=30s;
server 10.10.10.12:8080 max_fails=3 fail_timeout=30s;
}
The default method is round robin, while least_conn favours the server with fewer active connections. Use weight=2 for a more capable backend, and consider ip_hash only when the application genuinely requires client affinity. Session storage shared through Redis or a database is usually more flexible.
For teams integrating messaging services, the application nodes might also consume RabbitMQ. The RabbitMQ Fedora guide provides useful installation context, although the queue service itself should generally remain private and should not be exposed through the public proxy.
Configure Reverse Proxy Behaviour
Add a server block that listens on the required address and forwards traffic to the upstream group:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://application_pool;
proxy_http_version 1.1;
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;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}
The forwarded headers allow the application to record the original client address and protocol. Confirm that the application framework trusts these headers only from the proxy network; otherwise, clients could forge them.
Apply the configuration safely:
sudo nginx -t
sudo systemctl reload nginx
A reload keeps existing connections available while Nginx adopts the new worker configuration.
Open Firewall Ports
If firewalld is active, permit web traffic on the proxy:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Backend servers should normally allow port 8080 only from the proxy’s private address, not from every network. This restriction is especially important when servers are hosted across Australian cloud regions or connected through a shared corporate network.
For TLS, terminate HTTPS at Nginx with a certificate from a trusted authority, then decide whether traffic to the backends also needs encryption. Internal encryption may be required by company policy, sensitive workloads or data-sovereignty arrangements.
Test Distribution And Failover
Send repeated requests through the proxy and inspect backend logs:
for i in {1..10}; do
curl -sI http://app.example.com | grep -E 'HTTP/|X-Backend'
done
Adding an identifying response header temporarily can show which backend answered. Use curl -v when checking redirects, forwarded headers and connection reuse. DNS must point the application hostname to the proxy’s public address, and local /etc/hosts entries can assist during testing.
Stop one backend and confirm that requests continue through the remaining server:
sudo systemctl stop application-service
curl -I http://app.example.com
Nginx’s passive failure detection is useful, but it is not a full application health-check system. A backend can return HTTP 200 while its database or queue dependency is broken, so monitor an endpoint that reflects genuine service readiness.
Tune For Reliability
Set realistic connection limits, timeouts and log formats based on measured traffic. Review /var/log/nginx/access.log and /var/log/nginx/error.log, then forward them to a central logging platform with timestamps in a consistent timezone. Australian operations teams often coordinate maintenance in AEST or AEDT, so recording the timezone clearly avoids confusion during incident reviews.
For higher traffic, enable appropriate worker settings in /etc/nginx/nginx.conf:
worker_processes auto;
events {
worker_connections 4096;
}
The correct values depend on memory, file-descriptor limits and workload characteristics. A load balancer does not remove the need for application capacity planning, database monitoring or backups. Additional Linux troubleshooting material is available through practical Android and Linux notes when comparing command-line administration approaches.
Deployment Checks For Administrators
Use this short checklist before directing production traffic to the new proxy:
- Confirm both backend services respond directly on their private addresses.
- Run
nginx -tbefore every reload and retain a known-good configuration. - Permit HTTP or HTTPS on the proxy while restricting backend ports to trusted sources.
- Test one backend failure, one application error and a full proxy restart.
- Monitor latency, status codes, connection counts and disk space for log files.
Keep configuration in version control and document the backend addresses, DNS records, certificate renewal process and rollback command. A second Nginx proxy or cloud load-balancing layer may be necessary when the proxy itself becomes a single point of failure.
The practical operating pattern is straightforward: define a protected upstream pool, pass accurate request metadata, validate every change, and test failures before users encounter them. This gives a CentOS-based Nginx load balancer a dependable foundation for applications serving customers across Australia.