Setup Nginx as Reverse Proxy for Apache2 on Ubuntu 17.04 | 17.10

Apache2 is the most popular webserver in used today. Nginx is number (#2), but Nginx can also serve as a reverse proxy. This brief tutorial is going to show students and new users how to configure Nginx webserver as a reverse proxy for Apache2 on Ubuntu 17.04 | 17.10.

Both servers have their strengths and weaknesses, however, when combined and configured properly, they can provide a better experience for your audience.

In this post, we’ll install both Nginx and Apache2 servers.. then configure Nginx to listen on port 80 while apache2 listens on port 8080. They both can not listen on the same port. When web traffic comes in, Nginx will accepts and forwards to Apache2 at the backend.

When you’re ready to configure Nginx as reverse proxy for Apache2, follow the steps below:

Step 1: Install Nginx

Installing Nginx is easy.. to do that, run the commands below

sudo apt-get update
sudo apt-get install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx to always restart when the server boots up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Step 2: Configure Nginx as Reverse Proxy

First create a root folder for your domain ( For this post we’re going to be creating it in the /var/www/html called myproxy.

sudo mkdir -p /var/www/html/myproxy

Then change the permissions on the directory to allow Nginx and Apache2 to access the content.

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 775 /var/www/html/

Next, run the commands below to create a proxy file for the proxy site called myproxy.

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

Then add the block of codes in the file then save.

server {
    listen 80;
    listen [::]:80;

    proxy_redirect           off;
    proxy_set_header         X-Real-IP $remote_addr;
    proxy_set_header         X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header         Host $http_host;

    location / {

Save the file when done. This is a basic Nginx proxy configuration. No advanced settings added.

Next, enable the Nginx site by running the commands below

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

Step 3: Install Apache2

After configuring Nginx, run the commands below to install Apache2

sudo apt-get install apache2

After installing Apache2, run the commands below to change Apache2 default port to port 8080

sudo nano /etc/apache2/ports.conf

Then change the lines to look like the ones below:


Save the file and continue below

Step 4: Configure Apache2 Site

Now that Apache2 is installed, run the commands below to create the apache2 site.

sudo nano /etc/apache2/sites-available/myproxy.conf

Then add the content below into the file and save.

     ServerAdmin [email protected]
     DocumentRoot /var/www/html/myproxy/

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined


Save the file and exit.

Step 5: Enable Apache2 Site and Restart

After configuring Apache2, enable the site and restart. Run the commands below to enable the Apache2 site.

sudo a2ensite myproxy.conf

Then restart Apache2 by running the commands below

sudo systemctl restart apache2.service

Remove Apache2 default index.html file.

sudo rm /var/www/html/index.html

Go and test the settings by browsing to the domain of the site. Nginx should forward web traffic to Apache2 content.

That’s it!

You may also like the post below:

1 Comment

  1. can i use this guide for site that is hosted with wordpress?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.