0
0
Nginxdevops~5 mins

Backup servers in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a main server can fail or be busy. Backup servers help by taking over traffic automatically to keep websites running smoothly without interruptions.
When your main web server is overloaded and you want another server to help handle extra visitors.
When you want to keep your website online even if the main server crashes or goes offline.
When you want to test a new server setup without stopping the main server.
When you want to balance traffic between servers but prefer one as the main and others as backups.
When you want to improve reliability by having a standby server ready to serve requests.
Config File - nginx.conf
nginx.conf
http {
    upstream backend {
        server main-server.example.com weight=5 max_fails=3 fail_timeout=30s;
        server backup-server.example.com backup;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    }
}

This configuration defines an upstream group named backend with two servers.

The main-server.example.com is the primary server with a weight to handle most traffic.

The backup-server.example.com is marked as backup, so it only receives traffic if the main server fails.

The server block listens on port 80 and forwards all requests to the backend group.

Commands
This command tests the nginx configuration file for syntax errors before applying it.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command checks the HTTP response headers from the nginx server to verify it is running and serving requests.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

If the main server fails, nginx automatically sends traffic to the backup server to keep the site available.

Common Mistakes
Not marking the backup server with the 'backup' keyword in the upstream block.
Without 'backup', nginx will load balance traffic between servers instead of using one as a fallback.
Add the 'backup' keyword to the backup server line in the upstream block to make it a true backup.
Reloading nginx without testing the configuration first.
If the configuration has errors, nginx will fail to reload and the server may stop serving requests.
Always run 'nginx -t' to test configuration before reloading.
Summary
Define an upstream group with a main server and a backup server using the 'backup' keyword.
Test the nginx configuration with 'nginx -t' before applying changes.
Reload nginx to apply the new configuration without downtime.
Verify the server is running and serving requests with a simple curl command.