HiBlog

What Is a Reverse Proxy and How Does It Work?

  • Ali GhanimatiAuthor
  • Release Date
  • 5 minutesReading time
  • 7Views
  • Ali Ghanimati
  • Published:
  • 5 min read
  • 7 views
What Is a Reverse Proxy and How Does It Work?

Your Node.js or Python app is up on your VPS, but it listens on port 3000, and users have to open it at an odd address like yourdomain.com:3000, without HTTPS. Or your site has grown so busy that one server can’t keep up, and you want to split the load between two. The answer to both problems is the same: a reverse proxy.

In this article we explain what a reverse proxy is and how it routes a request, its benefits, how it differs from a regular proxy, and, with a real example, how to set one up with Nginx.

Short answer: a reverse proxy is a server that sits in front of a site’s real servers: every user request reaches it first, and it passes the request to the right server or app behind it and returns the response. With it you manage HTTPS in one place, spread load across several servers, cache responses and keep the real servers out of direct view.

What is a reverse proxy?

A reverse proxy is a middle layer between users and the real web server or apps. The user thinks they’re talking to the site directly, but they’re actually connected to the reverse proxy; the proxy sends the request to the backend server, gets the answer and hands it to the user. The backend servers never face users directly.

Users’ requests going from the internet to a reverse proxy and the servers of the internal network

How does a reverse proxy work?

  1. The user opens the site’s address, and DNS sends them to the reverse proxy’s IP, not the real server.
  2. The proxy receives the request and, if it’s HTTPS, decrypts it right there.
  3. Based on the domain, the path or the servers’ load, it decides which server or app gets the request and forwards it.
  4. The backend server builds the response and returns it to the proxy.
  5. The proxy gives the response to the user, compressed or cached if needed.

Benefits of a reverse proxy

  • Load balancing: requests are spread across several servers, and if one fails, the others answer.
  • HTTPS in one place: the SSL certificate is installed only on the proxy, and the apps behind it don’t have to deal with encryption.
  • Caching: frequent responses and static files are served from the proxy’s memory, taking load off the real server.
  • Better security: the real servers’ IPs and ports stay hidden, and suspicious traffic can be limited before it reaches them.
  • Several apps behind one IP: one proxy can route several domains or paths to different apps on different ports.

Where are reverse proxies used?

You deal with them more than you think. CDNs such as Cloudflare, when their proxy is on, are exactly a reverse proxy for your site. On a VPS, the most common use is the one from the start of this article: Nginx sits on ports 80 and 443 and passes requests to a Node.js, Python or Java app on an internal port. Big sites also use it to spread load across dozens of servers.

Reverse proxy vs. regular proxy

A regular (forward) proxy represents users: the computers of a network go out to the internet through it, and sites see the proxy’s IP, not the user’s. A reverse proxy represents servers: internet users connect to it, and the real servers’ IPs stay hidden.

FeatureRegular proxyReverse proxy
RepresentsUsersServers
Who stays hiddenThe userThe real server
Where it sitsNext to the users, in their networkIn front of the site’s servers
Common useControlling and caching a network’s internet accessLoad balancing, HTTPS, caching, security

Setting up a reverse proxy with Nginx

Say an app runs on the same server on port 3000. After installing Nginx, create a file such as /etc/nginx/sites-available/app:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

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

Enable the file, test the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

The proxy_pass line sends requests to the app, and the X-Forwarded-For and X-Real-IP headers pass the user’s real IP to it; without them, the app sees every request coming from 127.0.0.1. Now close the app off behind the firewall so port 3000 is only reachable from inside the server, and get an SSL certificate for the domain.

  • Nginx: light and fast; the most common choice on Linux servers.
  • Apache: with the mod_proxy module; handy if your server already runs Apache.
  • HAProxy: a load-balancing specialist with very high performance.
  • Caddy: simple configuration and automatic HTTPS certificates.
  • Traefik: built for Docker and Kubernetes environments.

FAQ

Behind a reverse proxy, how do I see users’ real IPs?

The proxy has to send the user’s IP in a header such as X-Forwarded-For, and the backend app or web server has to be set to accept that header only from proxies it trusts. With Cloudflare, the CF-Connecting-IP header does the same job.

Doesn’t a reverse proxy slow the site down?

There’s one extra hop, but when the proxy is on the same server or network, that delay is negligible. With caching and compression, a reverse proxy usually makes the site faster.

A reverse proxy on a HiData VPS

To set up Nginx and a reverse proxy you need root access, and a HiData VPS gives you exactly that: you install the operating system from the server panel and bring up any app behind Nginx. Want to try before you buy? Get the free 24-hour VPS!

Share Post

hidata.org/en/blog/what-is-a-reverse-proxy/
In Social Media

Add Comment

Related Articles