What if your website never went down, even when servers fail?
Why Backup servers in Nginx? - Purpose & Use Cases
Imagine you run a busy website with just one server handling all the visitors. One day, that server crashes or goes offline unexpectedly. Suddenly, your website is down, and visitors see error messages instead of your content.
Relying on a single server means if it fails, your whole site stops working. Manually switching to a backup server takes time and can cause long outages. This leads to frustrated users and lost business.
Backup servers automatically take over when the main server fails. This switch happens quickly and smoothly, keeping your website online without you lifting a finger.
server 192.168.1.10; # Manually change to backup IP if main fails
server 192.168.1.10 max_fails=3 fail_timeout=30s; server 192.168.1.11 backup;
Backup servers let your website stay online and reliable, even when problems happen behind the scenes.
A popular online store uses backup servers so customers can keep shopping even if one server crashes during a big sale.
Single servers can fail and cause downtime.
Manual switching is slow and error-prone.
Backup servers automatically keep services running smoothly.
Practice
backup in an upstream block in nginx?Solution
Step 1: Understand the role of backup servers
Backup servers are fallback servers used only when main servers fail.Step 2: Analyze the
Marking a server asbackupkeyword effectbackupmeans it will receive traffic only if main servers are down.Final Answer:
To use it only if all main servers are unavailable -> Option AQuick Check:
Backup servers = fallback only [OK]
- Thinking backup servers share load equally
- Assuming backup servers are always active
- Confusing backup with disabled servers
upstream block?Solution
Step 1: Recall nginx upstream server syntax
The correct syntax isserver [IP] [parameters];with parameters likebackupafter IP.Step 2: Identify correct placement of
Thebackupbackupkeyword comes after the IP address in the server line.Final Answer:
server 192.168.1.10 backup; -> Option CQuick Check:
Backup keyword after IP = correct syntax [OK]
- Placing 'backup' before IP address
- Using 'backup server' instead of 'server ... backup;'
- Omitting semicolon at line end
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?Solution
Step 1: Understand upstream with backup server
Main server 10.0.0.1 handles requests first; backup 10.0.0.2 is fallback.Step 2: Check server availability
Since 10.0.0.1 is up, nginx sends all requests there, ignoring backup.Final Answer:
10.0.0.1 only -> Option AQuick Check:
Backup used only if main down [OK]
- Assuming backup server shares load
- Thinking backup server is always active
- Believing nginx sends requests to both simultaneously
upstream backend {
server 192.168.0.1 backup;
server 192.168.0.2;
}What is wrong with this configuration?
Solution
Step 1: Validate syntax and parameters
All server lines have correct syntax, valid IPs, proper semicolons, and correctbackupusage.Step 2: Check server order
Position of backup servers does not matter; nginx ignores them unless all main servers fail.Final Answer:
Nothing is wrong with this configuration -> Option DQuick Check:
Backup position irrelevant [OK]
- Thinking backup servers must be listed after main servers
- Believing server order causes failures
- Assuming 192.168.x.x IPs are invalid
server1 and server2 normally, but use backup1 and backup2 only if both main servers fail. Which configuration correctly sets this up?Solution
Step 1: Identify main and backup servers
Main servers are server1 and server2; backup servers are backup1 and backup2.Step 2: Apply correct syntax and order
Main servers listed first withoutbackup, backups listed after withbackupkeyword.Step 3: Verify options
upstream backend { server server1; server server2; server backup1 backup; server backup2 backup; } correctly lists main servers first, backups withbackupkeyword after.Final Answer:
upstream backend { server server1; server server2; server backup1 backup; server backup2 backup; } -> Option BQuick Check:
Main servers first, backups last with 'backup' keyword [OK]
- Placing backup servers before main servers
- Missing 'backup' keyword on backup servers
- Marking main servers as backup by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
