How to Set Up Nginx as a Reverse Proxy for Apache

Nginx reverse proxy for Apache

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to set up Nginx as a reverse proxy server for Apache on Ubuntu and Debian. I will assume that you already have an existing Apache web server.

By the end of this tutorial, you will have a properly configured Nginx reverse proxy server that will handle incoming requests and pass them along to Apache via a socket.

1. Change Apache Listen Port

The first step is to change the port that Apache listens on. By default, most HTTP websites listen on port 80. If your website uses HTTPS, the default port is 443.

In either case, you will need to change the port that Apache listens on to something else. In this tutorial, I will be using port 8080.

Make this change in the following places.

  • /etc/apache2/ports.conf
  • /etc/apache2/your-website.conf

Apply your changes by reloading the Apache web server.

systemctl reload apache2

2. Install and Configure Nginx Reverse Proxy Server

If you don’t already have Nginx, you can install it with the apt package manager on Ubuntu and Debian.

apt install nginx

Once Nginx is installed, add the following proxy-related options to your Nginx configuration file. I’m using the default configuration files located at /etc/nginx/sites-available/default.

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

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
        proxy_pass http://YOUR-IP-OR-DOMAIN:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

Make sure that you replace YOUR-IP-OR-DOMAIN with your IP address or domain name.

Notice how the proxy_pass line references port 8080. This is how the Nginx reverse proxy server will communicate with the Apache server.

The other proxy_set_header lines add additional information to the response header.

Apply your changes by reloading Nginx.

systemctl reload nginx

3. Test Out the Reverse Proxy Server

Navigate to your IP address or domain name. Examine the response header with your browser’s developer tools. You will now see that your website is being served from Nginx instead of Apache.

Nginx as reverse proxy for Apache

I have an entire video which will also walk you through the process of setting up an Nginx reverse proxy server for Apache. Please let me know if you have any questions in the comments below. I’ll do my best to help you out.

YouTube video


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

One Response

  1. Hi sounds good as use .htaccess heavily but with hestia cp i am forced to use nginx.
    I cant seem to fathom out what files to change as they must be in different places. I have
    Hestia Control Panel:

    Operating System:
    Debian 10.10 (x86_64)

    Don’t know if you can shed any light on this – thanks

Leave a Reply

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