One WordPress server handles a quiet blog fine. It starts to struggle when traffic spikes, a plugin update goes wrong, or the server needs a reboot during business hours. An nginx load balancer wordpress servers setup fixes that by placing Nginx in front of two identical WordPress servers and splitting incoming requests between them. If one backend goes down, the other keeps answering. Here is the full setup.
Nginx Load Balancer WordPress Servers: How Traffic Flows Between Two Backends
Picture three servers. The first runs Nginx and nothing else. It takes every visitor request, picks one of the two WordPress backends, and forwards the request there. The backend builds the page and sends it back through Nginx to the visitor.
Nginx picks a backend with a method you choose in the config. Round robin alternates between the two servers. Least connections sends the next request to whichever backend has the fewest open connections, which works better when some requests are slow and others are fast. IP hash sends the same visitor to the same backend every time, which helps when a plugin stores session data in local files. The load balancer itself becomes a single point of failure, so plan a second one with a floating IP later if uptime demands it.
What You Need Before You Start
You need three servers: one load balancer and two backends. They can be small VPS instances, since the backends do the heavy work while the load balancer mostly passes bytes through. All three should run the same OS, Nginx, and PHP versions so behavior stays consistent. If you want a deeper walkthrough of the upstream block itself, the guide to setting up load balancing with Nginx upstream covers the core directives in detail.
Both backends must run the same WordPress version, theme, and plugins. Any difference means visitors get different pages depending on which backend answers, and that bug is hard to reproduce because it only shows up for some visitors.
- Three servers: one Nginx load balancer, two WordPress backends
- One database both backends can reach, on its own server or on one backend
- A way to sync the uploads folder between backends, such as rsync on a schedule or a shared NFS mount
- A domain name pointed at the load balancer, with a valid SSL certificate
- SSH access to all three servers and a backup of the live site before you change anything
Step 1: Prepare the Two WordPress Backends
Build the first backend as a complete, working WordPress site: Nginx, PHP-FPM, MariaDB or MySQL, and the site files. Test it on its own first. Log in to wp-admin, publish a test post, upload an image, and confirm the page loads. Only copy it to the second server once the first one works end to end.
Clone the first backend to the second server: web root, Nginx server block, and PHP-FPM pool config. The same cloning habits from setting up a staging copy of WordPress apply here, same files and database structure on a different host. Verify the second server answers on its own private address before the load balancer points at it. Update both backends together from here on.
Step 2: Point Both Backends at One Database and Shared Uploads
WordPress stores posts, users, and settings in the database, so both backends must read and write the same one. Point both wp-config.php files at the same database host, name, user, and password. If the database lives on one backend, open the database port to the other backend’s private IP only, never to the whole internet.
Uploads are the other shared piece. When an editor uploads an image through backend A, backend B must serve it too. The simplest fix is an rsync job that copies wp-content/uploads from one backend to the other every minute. An NFS share mounted at wp-content/uploads on both servers removes the delay, but if the NFS server stops, both backends lose uploads at once.
Also sync the rest of wp-content. Themes and plugins live on disk, not in the database, so a plugin installed on backend A only will break pages served by backend B. Apply plugin and theme changes to both backends.
Step 3: Write the Nginx Upstream Config on the Load Balancer
On the load balancer, define an upstream group with the private IPs of both backends, then proxy requests to it. Private IPs keep backend traffic off the public internet and cut latency. An nginx upstream wordpress servers block is the heart of the config, and this is the nginx reverse proxy wordpress pattern in its simplest form:
upstream wordpress_backends {
least_conn;
server 10.0.0.11:80 max_fails=3 fail_timeout=30s;
server 10.0.0.12:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://wordpress_backends;
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;
}
}
The max_fails and fail_timeout lines tell Nginx to stop sending traffic to a backend after three failed attempts and try it again after 30 seconds. That is what keeps the site up when one backend crashes or reboots. The proxy headers matter too: without X-Forwarded-For and X-Forwarded-Proto, WordPress sees the load balancer’s IP instead of the visitor’s, and it can build http links on an https site.
Before you reload Nginx, check the config. The guide to testing a new Nginx config without taking your site down shows how to validate with nginx -t and reload safely. A typo in an upstream block takes the whole site down at once, so never skip the test.
If your site relies on PHP sessions or a plugin that stores login state in local files, swap least_conn for ip_hash. That pins each visitor to one backend, so users stop getting logged out when their next request lands on the other server. The better long-term fix is moving sessions into the database or Redis, which lets you keep least_conn and still share state.
Step 4: Handle Sessions, Cache, and SSL
Terminate SSL on the load balancer. Install the certificate there with Certbot, listen on port 443, and redirect port 80 to https. The backends can talk plain http over the private network, which keeps the config simple and avoids certificate management on three servers. Add the usual wp-config.php check so WordPress treats forwarded https as secure.
Put page caching in one place, not two. If both backends run their own full-page cache, a purge on one backend leaves stale pages on the other. Either cache on the load balancer with Nginx FastCGI or proxy cache, or use a shared object cache both backends connect to. One cache, one purge path, no stale pages served from the forgotten backend. Some sites route /wp-admin to one backend only, which makes debugging easier.
Step 5: Test Failover and Watch the Logs
Test the setup by stopping PHP-FPM on one backend and loading the site. Pages should still load, just from the surviving server. Check response times while you are at it; if pages suddenly feel slow under load, the walkthrough for fixing slow TTFB on a VPS gives a methodical way to tell whether the bottleneck is the server, the database, or the cache. Then bring the backend back, stop the other one, and repeat. Test both directions, because a config that only fails over one way will surprise you at 2 a.m.
Watch the load balancer’s error log during the test. You should see Nginx mark the stopped backend as failed and route around it. If requests hang instead, the proxy timeouts are probably too long, or the backend is accepting connections and then stalling. Curl each backend directly to see which one hangs.
Once failover works, test the boring paths too. Upload an image in wp-admin and confirm it loads when served by the other backend. These are the paths that break quietly in a wordpress load balancing setup, and finding them now beats finding them from a user report.
Common Problems and Fixes
Visitors getting logged out on every other click usually means sessions are stored locally and requests are bouncing between backends. Switch to ip_hash first, then move sessions to a shared store when you have time. Mixed content warnings point at the X-Forwarded-Proto header or the wp-config.php https check; fix the header before chasing plugin settings. Images that load half the time point at an uploads sync gap, not the cache.
If one backend gets most of the traffic, check that both servers are actually in the upstream block and that the busy one is not failing health checks and recovering in a loop. Nginx logs on the load balancer show exactly how many requests each backend received, so start there instead of guessing. Most breaks in a setup like this come from shared state, uploads, or sessions, not raw CPU. Get failover working, keep both backends identical, and you have a site that survives a single server failure instead of going dark with it.