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 mydomain.com
sudo nano /etc/apache2/sites-available/mydomain.conf
Then copy and paste the lines below into the file and save.
<VirtualHost *:80> ServerName mydomain.com ServerAlias www.mydomain.com ServerAdmin webmaster@mydomain.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ <Location /> Order allow,deny Allow from all </Location> </VirtualHost>
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; server_name mydomain.com www.mydomain.com; 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

Enjoy!
You may also like the post below:
A big thanks for this clear procedure, everything worked well 🙂