Web
Difficulty: Intermediate
4 min read

Reverse Proxy: Apache, Nginx, or Traefik

A detailed tutorial on configuring a reverse proxy with Apache, Nginx, or Traefik on Linux to optimize and secure your infrastructure.

Back to tutorials
What Is a Reverse Proxy?
A reverse proxy is a server that sits in front of one or more web servers. It intercepts client requests and forwards them to the appropriate backend servers. To the client, the reverse proxy is completely transparent; it appears to be the final web server.

Why Use a Reverse Proxy?

  • Load Balancing: Distributes traffic across multiple backend servers to prevent overload and improve availability.
  • Security: Hides the identity and characteristics of the backend servers. It can also filter out malicious requests.
  • Centralized SSL/TLS: Handles HTTPS decryption at a single point (SSL termination), simplifying certificate management.
  • Caching: Can cache static content to speed up response times and offload the backend servers.
  • URL-based routing: Can direct `domain.com/api` to one service and `domain.com/blog` to another.

Choosing Your Tool

  • Nginx: The most popular choice. Extremely fast, lightweight, and its reverse proxy configuration is very simple. Ideal for most use cases.
  • Apache: Very powerful and flexible thanks to its many modules (mod_proxy), but its configuration can be more verbose. A good choice if you are already in an Apache ecosystem.
  • Traefik: The modern tool of choice for container-based environments (Docker, Kubernetes). It configures itself dynamically by detecting containers as they start.

Configuration with Nginx (Recommended)

This is the most common and often the simplest case.

Step 1: Install Nginx

sudo apt-get update
sudo apt-get install -y nginx

Step 2: Create a Configuration File

Create a file at /etc/nginx/sites-available/my-proxy:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        # The address of your backend service
        proxy_pass http://127.0.0.1:3000;

        # Headers to forward
        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;
    }
}

Step 3: Enable the Site and Reload Nginx

sudo ln -s /etc/nginx/sites-available/my-proxy /etc/nginx/sites-enabled/
sudo nginx -t # Check the syntax
sudo systemctl reload nginx

All requests arriving at `your-domain.com` will now be forwarded to the application running on port 3000 of the same machine.

Configuration with Traefik and Docker (Modern)

Ideal if your applications run inside Docker containers.

Step 1: Create the `docker-compose.yml` File

version: "3.3"

services:
  traefik:
    image: "traefik:v2.9"
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080" # Traefik dashboard
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  my-app:
    image: "containous/whoami" # A simple image that displays info about the request
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.my-app.rule=Host(`your-domain.com`)"
      - "traefik.http.routers.my-app.entrypoints=web"

Step 2: Start the Services

docker-compose up -d

That's it! Traefik automatically detected the `my-app` container through its labels and created the route. If you start other containers with similar labels, Traefik will add them dynamically.

Configuration with Apache (mod_proxy)

Step 1: Enable the Required Modules

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo systemctl restart apache2

Step 2: Configure a VirtualHost

Edit your site configuration file (e.g. /etc/apache2/sites-available/000-default.conf).



    ServerName your-domain.com

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/

Step 3: Reload Apache

sudo systemctl reload apache2
What About HTTPS?
The next step for a production reverse proxy is to enable HTTPS. With Nginx and Apache, you can use certbot from Let's Encrypt. With Traefik, it's even simpler: just add a few lines to its configuration so that it obtains and renews certificates automatically.

Conclusion

The reverse proxy is a fundamental component of modern web architecture. It brings security, performance, and flexibility. - Nginx is the versatile, high-performance choice for classic deployments. - Traefik is the king of automation in containerized environments. - Apache remains a viable option, especially if your existing infrastructure already relies on it. The choice depends on your architecture, but setting up a reverse proxy is almost always a good decision.

Written by

Morgann Riu

Cybersecurity and Linux administration expert. I share my knowledge through free tutorials and training to help system administrators and developers secure their infrastructures.

Share this tutorial

Did you enjoy this article?

Comments

Checklist Sécurité Linux

30 points essentiels pour sécuriser un serveur Linux. Recevez aussi les nouveaux tutoriels par email.

Pas de spam. Désabonnement en 1 clic.