How to Setup Nginx as Reverse Proxy Over Apache on Debian/Ubuntu

Nginx is a lightweight web server that has been proven to serve static files faster than Apache. This tutorial will guide you how to install Nginx as reverse proxy over Apache web server.

How to Setup Nginx as Reverse Proxy Over Apache on Debian/Ubuntu

How to Setup Nginx as Reverse Proxy Over Apache on Debian/Ubuntu

Requirements

You have installed Apache on your server. Apache is already running a site on port 80.

Change Apache listening port

Edit /etc/apache2/ports.conf to make Apache listening to port 8080 instead of default port 80.

Find following line:

Change it to:

Do not forget to your existing vhost listening port in /etc/apache2/sites-enabled/*

Change:

To:

Disable Unuse modules in Apache

Since HTTP requests is now handled by Nginx, we can disable KeepAlive in Apache. Edit /etc/apache2/apache2.conf and change:

Also, run following commands to disable unused modules.

Install forward module

Install mod_rpaf in Apache to forward visitor IP to Apache. Otherwise, your scripts will read REMOTE_ADDR values as server IP.

Stop Apache service

Setup Nginx

Install Nginx.

Remove default vhost to prevent conflicts.

Create a new default vhost:

Create vhost for existing website to forward request to Apache:

Restart Nginx and it’s done.