How to Configure a Reverse Proxy on Apache/Nginx

What is a Reverse Proxy? A reverse proxy is a server that sits in front of one or more web servers, intercepting requests from clients. It directs these requests to appropriate backend servers, effectively acting as an intermediary between clients and backend servers. Common uses for a reverse proxy

Last updated 30 Nov 2026

What is a Reverse Proxy?

A reverse proxy is a server that sits in front of one or more web servers, intercepting requests from clients. It directs these requests to appropriate backend servers, effectively acting as an intermediary between clients and backend servers. Common uses for a reverse proxy include load balancing, increased security, and caching.


Configuring a Reverse Proxy on Apache

Prerequisites

  • Apache HTTP Server installed on your system.
  • Root or sudo access to configure Apache.
  • The backend server(s) (the servers to which Apache will forward requests).

Step 1: Install Necessary Apache Modules

For reverse proxy configuration, Apache requires the mod_proxy, mod_proxy_http, and optionally, mod_ssl (for HTTPS support).

  1. Enable modules (if not already enabled):
sudo a2enmod proxysudo a2enmod proxy_httpsudo a2enmod ssl # Optional, for HTTPS

2. Restart Apache to load the modules:

sudo systemctl restart apache2

Step 2: Configure Reverse Proxy

  1. Edit the Apache configuration file for your site. You can modify the default configuration file or create a new virtual host file in /etc/apache2/sites-available/:
sudo vi /etc/apache2/sites-available/your_site.conf

2. Set up ProxyPass directives within your virtual host to specify how Apache should handle requests.

  • Here’s an example configuration to set up a reverse proxy to a backend server at http://backend_server_ip:port:
<VirtualHost *:80>
    ServerName yourdomain.com

    # Proxy settings
    ProxyPass "/" "http://backend_server_ip:port/"
    ProxyPassReverse "/" "http://backend_server_ip:port/"

    # Optional - Set up ProxyErrorOverride to show error pages on Apache instead of backend errors
    ProxyErrorOverride On

    # Additional security headers
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

3. Save and close the file.

4. Enable the site (if it’s a new virtual host configuration) and restart Apache:

Step 3: Verify Reverse Proxy Setup

  • Access http://yourdomain.com in a web browser. It should display the content served by your backend server, confirming that the reverse proxy is functioning.

Configuring a Reverse Proxy on Nginx

Prerequisites

  • Nginx server installed on your system.
  • Root or sudo access to configure Nginx.
  • Backend server(s) to forward requests to.

Step 1: Configure Nginx for Reverse Proxy

  1. Edit the Nginx configuration file. Nginx typically stores site configuration files in /etc/nginx/sites-available/. Modify the default file or create a new file for your domain:
sudo nano /etc/nginx/sites-available/your_site.conf

2. Add a server block with proxy settings. Replace backend_server_ip:port with your backend server’s address.

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://backend_server_ip:port;
        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;
    }
}

Explanation of key directives:

  • proxy_pass: Forwards client requests to the backend server.
  • proxy_set_header: Preserves the original client headers, which can be useful for logging or authentication.

3. Save and close the file.

4. Enable the site by creating a symbolic link to sites-enabled (if necessary):

sudo ln -s /etc/nginx/sites-available/your_site.conf /etc/nginx/sites-enabled/

5. Test the Nginx configuration to ensure syntax correctness:

sudo nginx -t

6. Restart Nginx to apply the changes:

sudo systemctl restart nginx

For added security, set up SSL on the reverse proxy. You can use a tool like Let’s Encrypt to obtain SSL certificates for free.

  1. Install Certbot (if not already installed):
sudo apt install certbot python3-certbot-nginx

2. Obtain and install the SSL certificate:

sudo certbot --nginx -d yourdomain.com

3. Verify that HTTPS works by accessing https://yourdomain.com.

Step 3: Test the Nginx Reverse Proxy

  • Go to http://yourdomain.com or https://yourdomain.com in a browser. You should see the content served by your backend server, indicating the reverse proxy is functioning correctly.

Conclusion

By following this guide, you can set up a reverse proxy on either Apache or Nginx to forward client requests securely and efficiently to backend servers. This configuration allows you to load balance traffic, enhance security, and manage resources more effectively within your network.

Was this article helpful?