Bird
Raised Fist0
Nginxdevops~5 mins

Backup servers in Nginx - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of marking a server as backup in an upstream block in nginx?
easy
A. To use it only if all main servers are unavailable
B. To prioritize it over main servers for all requests
C. To permanently disable the server
D. To balance load equally with main servers

Solution

  1. Step 1: Understand the role of backup servers

    Backup servers are fallback servers used only when main servers fail.
  2. Step 2: Analyze the backup keyword effect

    Marking a server as backup means it will receive traffic only if main servers are down.
  3. Final Answer:

    To use it only if all main servers are unavailable -> Option A
  4. Quick Check:

    Backup servers = fallback only [OK]
Hint: Backup servers handle traffic only when main servers fail [OK]
Common Mistakes:
  • Thinking backup servers share load equally
  • Assuming backup servers are always active
  • Confusing backup with disabled servers
2. Which of the following is the correct syntax to define a backup server in an nginx upstream block?
easy
A. server 192.168.1.10;
B. server backup 192.168.1.10;
C. server 192.168.1.10 backup;
D. backup server 192.168.1.10;

Solution

  1. Step 1: Recall nginx upstream server syntax

    The correct syntax is server [IP] [parameters]; with parameters like backup after IP.
  2. Step 2: Identify correct placement of backup

    The backup keyword comes after the IP address in the server line.
  3. Final Answer:

    server 192.168.1.10 backup; -> Option C
  4. Quick Check:

    Backup keyword after IP = correct syntax [OK]
Hint: Put 'backup' after IP in server line [OK]
Common Mistakes:
  • Placing 'backup' before IP address
  • Using 'backup server' instead of 'server ... backup;'
  • Omitting semicolon at line end
3. Given this nginx upstream block:
upstream backend {
    server 10.0.0.1;
    server 10.0.0.2 backup;
}

Which server will nginx send requests to if 10.0.0.1 is up?
medium
A. 10.0.0.1 only
B. 10.0.0.2 only
C. Both 10.0.0.1 and 10.0.0.2 equally
D. No server, error occurs

Solution

  1. Step 1: Understand upstream with backup server

    Main server 10.0.0.1 handles requests first; backup 10.0.0.2 is fallback.
  2. Step 2: Check server availability

    Since 10.0.0.1 is up, nginx sends all requests there, ignoring backup.
  3. Final Answer:

    10.0.0.1 only -> Option A
  4. Quick Check:

    Backup used only if main down [OK]
Hint: Backup server used only if main server fails [OK]
Common Mistakes:
  • Assuming backup server shares load
  • Thinking backup server is always active
  • Believing nginx sends requests to both simultaneously
4. You have this nginx upstream configuration:
upstream backend {
    server 192.168.0.1 backup;
    server 192.168.0.2;
}

What is wrong with this configuration?
medium
A. Backup keyword is misspelled
B. No semicolon after server lines
C. IP addresses are invalid
D. Nothing is wrong with this configuration

Solution

  1. Step 1: Validate syntax and parameters

    All server lines have correct syntax, valid IPs, proper semicolons, and correct backup usage.
  2. Step 2: Check server order

    Position of backup servers does not matter; nginx ignores them unless all main servers fail.
  3. Final Answer:

    Nothing is wrong with this configuration -> Option D
  4. Quick Check:

    Backup position irrelevant [OK]
Hint: Backup servers can be listed anywhere [OK]
Common Mistakes:
  • Thinking backup servers must be listed after main servers
  • Believing server order causes failures
  • Assuming 192.168.x.x IPs are invalid
5. You want nginx to send traffic to server1 and server2 normally, but use backup1 and backup2 only if both main servers fail. Which configuration correctly sets this up?
hard
A. upstream backend { server backup1; server backup2; server server1; server server2; }
B. upstream backend { server server1; server server2; server backup1 backup; server backup2 backup; }
C. upstream backend { server server1 backup; server server2 backup; server backup1; server backup2; }
D. upstream backend { server server1; server backup1; server server2; server backup2; }

Solution

  1. Step 1: Identify main and backup servers

    Main servers are server1 and server2; backup servers are backup1 and backup2.
  2. Step 2: Apply correct syntax and order

    Main servers listed first without backup, backups listed after with backup keyword.
  3. Step 3: Verify options

    upstream backend { server server1; server server2; server backup1 backup; server backup2 backup; } correctly lists main servers first, backups with backup keyword after.
  4. Final Answer:

    upstream backend { server server1; server server2; server backup1 backup; server backup2 backup; } -> Option B
  5. Quick Check:

    Main servers first, backups last with 'backup' keyword [OK]
Hint: List main servers first, backups last with 'backup' keyword [OK]
Common Mistakes:
  • Placing backup servers before main servers
  • Missing 'backup' keyword on backup servers
  • Marking main servers as backup by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard