1. Home
  2. Linux
  3. General
  4. How to configure Nginx as a Reverse Proxy for Apache

How to configure Nginx as a Reverse Proxy for Apache

How to configure Nginx as a Reverse Proxy for Apache
How to configure Nginx as a Reverse Proxy for Apache

Why using Nginx as a reverse proxy to Apache

Apache and Nginx are both very powerful and effective servers.
Apache leads as the #1 server for websites and, since it is on public release in 2006, Nginx is gaining rapidly and is currently the #2 server for active sites.
The reasons for each respective server’s popularity is clear such as Apache’s power and Nginx’s speed. However, each of these servers has its drawback; Apache is hard on server memory, while Nginx requires the help of php-fpm or other close modules for dynamic content.
There is a way to combine the two web servers to get great effect, with Nginx as its static web server front and then Apache processing the back end.
If you are interested in purchasing a web server you could do so from us at the link below.
https://site.dreamvps.com/vps-hosting/

Setup

In order to follow the steps in this tutorial, you must have sudo privileges on your virtual private server.
If you would like to know how to create a user with sudo privileges, follow the third and fourth steps of our Ubuntu Server Setup tutorial.

Step 1: Installing Nginx

To begin the tutorial, you must install and then configure Nginx; its job is going to be serving our front end of the site.
Use apt-get to download it as below.

sudo apt-get install nginx

After this has been downloaded, you may go ahead and start configuring the virtual host to run on the front end.
There are a few changes you must do for the configuration.

Step 2: Configuring Nginx

Open up the Nginx configuration.

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

The configuration shown should set you up to use Nginx as the front end server. The setup is very similar to the default set up, you will have the details under the configuration.

server {
 listen 80;
root /var/www/;
 index index.php index.html index.htm;
server_name example.com;
location / {
 try_files $uri $uri/ /index.php;
 }
location ~ \.php$ {
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $remote_addr;
 proxy_set_header Host $host;
 proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
 deny all;
 }
}

The list of changes below are the ones that were implemented in the configuration:

  • The root was set to the correct web directory
  • php was added on the index line
  • try_files attempts to serve whatever page the visitor requests. If nginx is unable, then the file is passed to the proxy
  • proxy_pass lets nginx the address of the proxied server
  • Finally the ‘location ~ /\.ht {‘ location block denies access to .htaccess files, if Apache’s document root concurs with nginx’s one

This configuration is a set up on the system where every extension with a php ending is rerouted to the Apache backend running on port 8080.
You can now activate the virtual host.

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example

Next, remove the default Nginx server block.

sudo rm /etc/nginx/sites-enabled/default

In the next step you will install and configure Apache.

Step 3: Apache

Since you have Nginx already taken care of, you are now going to proceed over to installing your backend, Apache.

sudo apt-get install apache2

As Nginx is still turned off, Apache is going to start running on port 80.

Step 4: Configuring Apache

You have to configure Apache so that it takes over the backend in the same way that we told Nginx; it will be ran on port 8080.
Open up the Apache ports file to begin setting Apache on its correct port.

sudo nano /etc/apache2/ports.conf

Search and replace the lines in the below in order to get Apache running on port 8080, only accessible from the localhost.

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Save and exit.
Afterwards, open a new virtual host file, copying the layout from the default apache file.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example
sudo nano /etc/apache2/sites-available/example

There is an issue that has to be addressed here, the virtual host has to be once more running on port 8080, replacing the default port 80 that Nginx has.
It should look like the line below.

<VirtualHost 127.0.0.1:8080>

Ensure that your Document Root is correct. Save and Exit the file before activating the virtual host.

sudo a2ensite example

Now before beginning to test things out, you must equip Apache with php. Use the following command to install it now.

sudo apt-get install php5

Next, restart both servers and cause the changes to be effective.

sudo service apache2 restart
sudo service nginx restart

Finish Up

Now you have set up the VPS with Nginx running on the front end of your site and Apache then processing php on the back end of it.
Loading your domain should take you to the site’s default page.
You can check if information is being routed to Apache successfully by simply running a common php script.
Create the ‘php.info’ file.

sudo nano /var/www/info.php

Then paste the lines below into the file.

<?
phpinfo( );
?>

Save and exit.
When you visit ‘domain/info.php’ it will show your php info screen, and you should be able to see that it was handled by Apache.
Now you can see which ports are open and what application is on each one by entering the command below.

sudo netstat –plunt

Conclusion

When you configure Nginx and Apache together it can offer a great, simple boost to a server.

Updated on December 24, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']