How to Setup Nginx as Reverse Proxy For WordPress ?

By using Nginx as a reverse proxy you can increase the speed of your website and cache your pages more effectively. In contrast to Squid and Varnish reverse proxy setups, Nginx has a simple and easy to understand configuration methodology. It is debatable whether Nginx is faster or slower than others, but we care more about the importance of easy configuration. The primary reason is that if you don’t understand how reverse proxies work than you will face tons of unimaginable problems and unfortunately these problems often arise more in Varnish and Squid. Nginx offers definitely a more simple configuration.

Simple Nginx Setup For Centos

It takes about 2 seconds to install Nginx to Centos, if you don’t want to deal with compiling from Nginx source. If you prefer compiling it, then obviously you will get the most recent version. In this article, we will not go into details of compiling Nginx, so we will use the version which comes with EPEL release.

Once the installation is complete, it is time to edit configuration files by using an editor such as nano or vi.

Even though the main configuration file contains many important parameters, we will not discuss them here since our goal is to setup a simple reverse proxy and not go into details of Nginx too much. Since the reverse proxy configuration will be made in a different file, we will just use the main configuration file below.

In the file above, the worker_process determines how many CPUs can be used by Nginx in multi-thread fashion. If you run the following command below, you can get learn how many CPUs you have.

For now, it is up to you to play with other parameters. For example, you can turn on/off error logs or modify the values for GZIP. As we have previously stated, our goal is to create reverse proxy configuration, which is defined in another file.

What Does a Reverse Proxy Do ?

Above, we have installed Nginx to Centos 6.6 (or 7) and simply tweaked a few options. Before we start to setup the reverse proxy, we believe it is more important to explain the logic of reverse proxies. If you don’t understand the logic, you will definitely face with tons of problems. So let’s start:
The primary goal of reverse proxy is to provide an end-user with the cached version of a page. If we use a WordPress site as an example, a WordPress page is created by rendering information gathered from its database server. If this site is hosted on Apache, then a new PHP process will be created by Apache for each user that arrives to this page. Obviously, when the number of users visiting your site increases then the number of processes increase and inevitably this adds more load to your server.
The goal of reverse proxy is to cache a page so that it does not go through this energy consuming process and thereby improve performance dramatically. It is clearly logical to keep this article, which you are reading right now, in a cache for some period since it is highly unlikely for it to be modified frequently. It does not make sense from a performance point of view to visit the database and render a page each time someone visits it. On contrary, if this page were dynamic, it would illogical to cache it since it will be changed frequently.
If you look from this perspective, a reverse proxy configuration is all about which pages should be cached for how long, and which don’t need to be cached at all and should be retrieved directly from the main source.

How Can Nginx Cache A Website On Apache Server ?

If a website is on Apache (it can be IIS or Nginx too), which answers requests from port 80, then our goal is to change this port to something different so that the reverse proxy can safely operate on port 80. By doing so, reverse proxy will forward incoming request to Apache’s new port.
Go ahead and change the port of Apache from 80 to 82 by editing httpd.conf and then restarting apache on your Centos.

Virtual.conf Setup for Nginx Reverse Proxy

The configuration file below is created to cache a WordPress website. Go ahead and clear default configuration files that come with the Nginx installation by running the commands below:

Now copy the configuration file below to virtual.conf:

From the comments inside the configuration above, you can easily understand that our goal is forwarded all requests to port 82 and then determine which pages should be cached or not. Once you save this file, and start Nginx you have a working reverse proxy. From the SEO point of view, it is logical to close public access to port 82 by editing IPTables and reloading it.

If everything goes well, based on the configuration above, you should be able to see that the directory below is getting larger and larger each time Nginx caches something:

You can test the configuration by installing a chrome extension such as HTTP Spy to investigate headers returned from Nginx. Simply, if a page is cached, you will see HIT, if it is not then MISS, if it should not be cached at all (like wp-admin directory) you’ll see BYPASS on the header response.

We hope this article saves a lot of your time in your efforts to create a NGINX reverse proxy.