In this tutorial we will cover the steps needed to set up nginx as a reverse proxy for the Apache web server on your CentOS 6.4, Debian or Ubuntu platform.

Requirements

  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • root privileges
  • Basic skills for working on a Linux environment
  • nginx and Apache HTTP services installed on the computer/server – How to install Apache/nginx

This is very useful configuration which might help you increase drastically the loading speed of your website. The nginx will server the static content (images, CSS and JS files, HTML) while the dynamic files will be handled by the Apache web server.

The nginx service will listen on the default HTTP port 80 while the Apache server will be configured with port 8080.

Configure nginx

We will start configuring the nginx service:

After that create new virtual host file:

Debian/Ubuntu:

Open the nginx configuration file and edit the lines inside it the following way:

After that create new virtual host file:

When ready save the file and copy it inside the sites-enabled/ folder:

Configure Apache

CentOS:

Open the Apache configuration file:

At the end of the file you should add the following lines:

The important variables are:

  • DocumentRoot - represents the default web root folder where your website files should be placed.
  • ServerName – represents the domain name assigned to the virtual host
  • ServerAlias – you can place more than one domain name (aliases) which you want to be assigned to that virtual host

Debian/Ubuntu:

First we will configure the Apache service to listen on port 8080:

change the default port from 80 to 8080:

Save and close the file. The next file which should be edited is:

Perform the same operation inside it and set port 8080 as default:

When you are ready save and close the file.

Create the following file which will contain the virtual host configuration:

When ready save the file and copy it to:

When both services are configured with the correct settings we should restart nginx and Apache in order for the changes to take effect:

CentOS 6.4

Debian/Ubuntu:

Test the functionality of the reverse proxy

You can test the functionality of the reverse proxy service using the following command:

You should receive output similar to the following one:

In order to check if the dynamic content is passed to the Apache server you can create a simple PHP information file inside the default web root folder of your website:

In case that yourdomain.com is pointed to the server's IP address you should be able to access the file through:

On the PHP information page you should be able to see:

An easy way to find your server's IP address is to execute the following command: