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

This brief tutorial shows students and new users how to setup Apache2 webserver as a reverse proxy for Nginx webserver. A reverse proxy is a type of service where the proxy server (frontend server) takes HTTP(S) requests and forwards them to a backend server.

In this setup, Apache2 will sit in front of Nginx webserver and accepts all requests. Then those request are transparently sent to Nginx webserver to fetch and retrieve the content. In our previous post, we showed you how to setup Nginx as a proxy server to Apache2.

You can find our previous post by clicking this link.

To get started with setting up Apache2 as a reverse proxy, follow the steps below

Step 1: Install and Configure Apache2

To get Apache2 as a reverse proxy, you must first install Apache2. To do that run the commands below

sudo apt-get install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to automatically start up everytime the server starts.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 2: Enable Apache2 Proxy

Now that Apache2 is installed, run the commands below to enable its proxy modules.

sudo a2enmod proxy
sudo a2enmod proxy_http

When you’re done, restart Apache2 to enable reload the modules.

sudo systemctl restart apache2.service

After that, run the commands below to create a new site configuration file called mydomain.conf for website

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

Then copy and paste the lines below into the file and save.

    <VirtualHost *:80>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyRequests Off
        <Proxy *>
          Order deny,allow
          Allow from all
        ProxyPass /
        ProxyPassReverse /

        <Location />
          Order allow,deny
          Allow from all


After that, save the file.

Step 3: Install and Configure Nginx

Now that Apache2 and configured, continue below to install Nginx. To do that run the commands below.

sudo apt-get install nginx

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

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

Nginx, run the commands below to create the Nginx site configuration file called mydomain.

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

Then copy and paste the content below into the file and save.

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;
    root /var/www/html/mydomain;
    index  index.php index.html index.htm;

    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;


Save the file when you’re done.

Step 4: Enable both the Apache2 and Nginx Site

sudo a2ensite mydomain.conf

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

Restart both Apache2 and Nginx

sudo systemctl restart apache2.service
sudo systemctl restart nginx.service

If Nginx fails to start, you may want to delete the default configuration for Nginx or change the port in the file to 8080.

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

Now put an index.html file into the root directory of the Nginx site.

Run the commands below to see services running on the system.. in the image, you’ll see Apache2 running on port 80 and Nginx on 8080

apache2 proxy to nginx


You may also like the post below:

Install HHVM on Ubuntu 17.04 / 17.10