Load balancing WordPress with Nginx

In previous post, I discussed about setting up WordPress blog on raspberry pi. This article explains load balancing requests using Nginx in front as reverse proxy. Load balancing across multiple instance of application is a way to maintain the availability and performance of WordPress.

Load balancing is more than just throwing in a load balancer. There are other things to consider and separate out. In this example, we will setup Nginx on one server which will act as load balancer and two instance of application (WordPress) on other two servers. I have kept MySql database on one of web server to keep things simple and easy to follow from previous article.

WordPress saves content such as images, config and plugins in file system. We also need a network location to share the files between server so we don’t have to replicate the them between web servers.

Server Setup Overview

This article assumes the use of following servers. All servers are Setup on Raspberry Pi 3+ running latest version of Raspbian Stretch Lite (debian based distribution for ARM architecture).

  • Load Balancer
    • Private I.P
    • Nginx
  • Web Server 1
    • Private I.P
    • Apache Web Server
    • MySql Database (Ideally you want a dedicate database host)
  • Web Server 2 (
    • Private I.P
    • Apache Web Server
  • Samba Server
    • Private I.P

Install Nginx

Lets begin by installing Nginx web server. Ssh to the host where you want to setup the load balancer. This example uses host with I.P

sudo apt-get install nginx
sudo apt-get install nginx

post installation you will need to start the nginx service

sudo service nginx start

Point your browser to from any computer in network. The browser will show default Nginx page which means that the installation is successful and web server is listening to port 80

Hello from nginx!
Nginx default web page

Setting Nginx as HTTP load balancer

In order to configure the Nginx for load balancing, we need to define few special directive in server block configuration of Nginx. Nginx create one server block named default. We can modify the same server block for our example. Open the default server block file located at /etc/nginx/sites-available/ in a text editor such as nano.

sudo nano /etc/nginx/sites-available/default

remove everything in default configuration file and enter the following. Least there are two directives upstream and server

upstream wpapp {         
 server {         
     listen 80;        
     location /  {             
         proxy_pass http://wpapp;         

The server directive specifies that http requests to port 80 will be proxied to the server group wpapp. In this example we are load balancing between 2 instances of the application running on and If unspecified, load balancing method defaults to round-robin. Save the file and exit (Cntrl+X and Enter). Enter the following command to check if there is any syntax problem with Nginx configuration and then restart Nginx service.

sudo nginx -t
sudo service nginx restart

Setup application instances

In earlier post, I’ve setup the WordPress application on single instance, we can follow same steps on our second instance except installing and setting up database. We don’t need new database. WordPress should connect to same database which is running on This way both application instances will be pointing to this database.

Ideally you may want to setup database on its own dedicated host/server but I am just leaving that part for the sake of simplicity and continuation.

Please note that there are chances that you’ll not be able to connect database from That is caused by default Mysql configuration which doesn’t let MySql server to connect to remote clients. Lets make changes in order to connect Mysql remotely.

Ssh to Open up the configuration file

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

and change the line

bind-address           =
#bind-address          =

restart the Mysql service

sudo service mysql restart

Now as the WordPress is setup on second instance, we are ready with our little load balancing setup. Point to your browser to The request will be forwarded to one of the running instance of application in round-robin fashion. We however still need to add a network file location for sharing plugins, themes and media files between application instances.

1 thought on “Load balancing WordPress with Nginx”

Leave a Comment