How To: Configure Nginx As A Reverse Proxy
I previously wrote about why page load times are important and outlined a couple of ways you could improve your web servers performance with regards to page load time. One of the ways I mentioned was to use nginx as a reverse proxy to serve all of your static files while letting Apache handle all of the dynamic content. There are numerous reasons for doing this with your server. First, nginx has a very small memory footprint and second, it can serve static files very quickly.
The following instructions are the steps I took for installing and configuring nginx with Apache on Debian.
Install nginx:
apt-get install nginx
Once nginx is installed (don’t start it yet) we need to edit the configuration file. My nginx configuration file handles multiple domains because I host multiple sites on my server. In the link below is an excerpt of my configuration file which displays nginx handling two domains. I should also note you can add as many domains as you want into the configuration file.
The configuration file should be located in /etc/nginx/nginx.conf – to edit it simply use your favorite editor such as nano.
View nginx Example Configuration
When your configuration file is saved, you will need to edit your Apache ports.conf file to point the listen port to 8080. It should look like the excerpt below:
NameVirtualHost *:8080
Listen 127.0.0.1:8080
You may also additionally have to edit each Apache sites-enabled configuration file to point the site to port 8080 as opposed to 80 which nginx will be listening on – this is to avoid conflicts.
Once you have completed the edits you may want to install the Apache mod libapache2-mod-rpaf so all of your Apache access logs do not show 127.0.0.1 as the IP for who is accessing your server. To install this mod issue the following command:
apt-get install libapache2-mod-rpaf
After the mod is installed it should restart Apache, however, if it doesn’t, you can issue the command yourself:
/etc/init.d/apache2 restart
Finally, you will want to start up nginx using the following command:
/etc/init.d/nginx start
That should do it! If you have any problems and/or questions feel free to leave a comment.