Build a serious WordPress server for high traffic website with nginx

November 10, 2011 by
Filed under: design, linux, server 

Deploy multiple nginx instance on 1 server for load balance

When building WordPress for high traffic website, we maintain and restart the web server frequently for better SEO  and better user experience; but we don’t want to interrupt user’s visiting.

When we get familiar with SEO, the link/site structure may change and need to update nginx configuration which need restart of nginx. When nginx new version released for fixing a dangerous security issue, we need upgrade nginx.

So we need at least 2 nginx instance for load balance, and another nginx as proxy server. Let’s build the 3 nginx instance on a single server with the same nginx installation(or use 2 nginx installation if need upgrading nginx ). Two of the instance serves WordPress with the same WordPress installation and the same database.

The architecture and network composition:


nginx load balance architecture on a single machine

nginx load balance architecture on a single machine













The following steps describes how to do in on Debian.

1. Deploy single wordpress server on nginx

Make sure your system is up-to-date.  Reference the following links to install php pre-requirements:

cd /opt


tar -zxvf nginx-1.0.9.tar.gz
cd /opt/nginx-1.0.9/
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6

make install

Create init script to manage nginx:

wget -O
mv /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults
/etc/init.d/nginx start

2. Duplicate nginx configurations

cd /opt/nginx  (change directory to where nginx is installed)

cp conf/nginx.conf  conf/nginx.81.conf

cp conf/nginx.conf  conf/nginx.82.conf

mv conf/nginx.conf conf/nginx.backup.conf

And then change the listen port 80 to 81,82 respectively in nginx.81.conf and nginx.82.conf; also don’t forget to comment out the pid line and change the pid file for each instance so that each instance can be stopped normally:

pid        logs/;  ### in file nginx.81.conf

pid        logs/;  ### in file nginx.82.conf

3. Configure and deploy the proxy nginx server

vi conf/nginx.conf,

copy the following content to listen on port 80 which is balanced by two nginx instance at port 81 and 82.

#deploy multiple nginx instance for load balance on 1 server

upstream main {

server {

location /   {
	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_pass http://main;

4. Stop your old server and start the new instance with load balance support

sbin/nginx -s stop, or kill -QUI pid

Then you can start the new instance by:

cd /opt/nginx
sbin/nginx -c conf/nginx.81.conf
sbin/nginx -c conf/nginx.82.conf
sbin/nginx -c conf/nginx.conf

In case you want to stop the instance, you can run one/all of these commands:

sbin/nginx -s stop -c conf/nginx.81.conf
sbin/nginx -s stop -c conf/nginx.82.conf
sbin/nginx -s stop -c conf/nginx.conf

Of course, you can stop the instance by executing this command too: kill -QUIT pid-of-the-nginx

5. Debug and maintenance one of the nginx instance

When you debugging on one of the nginx node, if you visit, WordPress will redirect your url to the default WordPress site URL  automatically(for example ), and then you don’t know which instance you are visiting.

But why wordpress has automatic url redirects? You can typically visit the home page of a WordPress web site by several different URLs:,,

The problem with allowing all of these URLs to access a single page is that it can potentially hurt your website’s overall search engine optimization (SEO). It means search engines could index duplicate copies. So WordPress fixes this problem by employing automatic redirects known as Canonical URL Redirection, which only enables one url per page.

When debugging and testing new functions,   you don’t want to enable the redirection. You can add the following code to functions.php file. remove_filter('template_redirect','redirect_canonical');

If you don’t like editing the file, you could install the plugin: “Permalink Fix & Disable Canonical Redirects Pack“, and activate it, then redirection would be disabled.

After debugging and testing of your WordPress finished, you should deactivate the plugin to enable the Redirection for a better SEO site.

6. Redirect request to next server if error or timeout.

When in load balance mode, nginx will redirect/resend request to another server by default when server error or timeout; for more error processing, we can leverage the directive of proxy_next_upstream and  fastcgi_next_upstream

syntax: proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off]; default: proxy_next_upstream error timeout;

context: http, server, location

The directive determines in what cases the request will be transmitted to the next server, here’s an example config:

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;  

fastcgi_next_upstream error timeout invalid_header http_500 http_503 http_404;

Digg This
Reddit This
Stumble Now!
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


One Comment on Build a serious WordPress server for high traffic website with nginx

  1. 学生手机网 on Mon, 14th Nov 2011 9:07 pm
  2. Mark一下,再来细读