How to create a reverse proxy with Nginx

Updated on 2026-10-09 03:01:05 51 visits

In this tutorial, we will show you how and why to create a so-called "reverse proxy" with the popular web server Nginx; nowadays, it is the most popular option for this type of task.

Before starting the tutorial, do you not have a Cloud Linux server where you can install your nginx web server? Don't worry! With just one click, you can easily deploy it with SW Hosting.

What is a reverse proxy?

A reverse proxy refers to a server in charge of displaying content from one or more servers that, in turn, remain hidden from the client or user.

info For example, if you place a reverse proxy in front of a web application, your users will never interact directly with the application; instead, the reverse proxy will be responsible for acting as an intermediary between your users and the application.

Reverse proxies can be an option to increase security. For example, they allow us to hide the actual server where your applications are hosted or also allow us to implement additional security layers, such as a TLS connection applied by the proxy, which might not have been natively supported by your application.

Likewise, a reverse proxy is practical for serving multiple web applications from the same machine. The application generally runs on localhost on uncommon ports such as 4000 or 5000. Then, the reverse proxy will listen on ports 80/443 and serve the content of one web application or another according to the SNI or the host of the request.

Creating a reverse proxy with Nginx, step by step

First, you must meet the following requirements:

  1. Have root access to a Linux server.
  2. Have Nginx installed and activated. Ensure that ports 80 and 443 are not occupied by another process, such as Apache.
  3. Have a web application running on localhost via an arbitrary and available port (for example, port 4000). This can be easily achieved with NodeJs or .NET Core.

1. Create a new virtual host file

You can achieve this using the nano text editor. A good path to do this is sites-available within the nginx configuration directory.

nano /etc/nginx/sites-available/mydomain.com.conf

2. Edit the file and add the configuration for the reverse proxy

file mydomain.com.conf

server {
  # Listens on port 80, ipv4.
  listen 80; 
  
  # Here you should enter your domain name.
  server_name mydomain.com;

  access_log            /var/log/nginx/mydomain.com.access.log;

  location / {
      # The proxy configuration.
      proxy_pass http://localhost:4000/;
  }
}

3. Restart Nginx

For Debian-based operating systems:

systemctl reload nginx

4. Launch your web application

At this point, you should run your web application if it is not already running.

In the long term, you will want to run it as a service/domain, but for testing, running it manually will be sufficient.

For example, if you have a NodeJS application, the syntax is as follows:

node /path/to/application.js

5. Access your web application from the browser

If you followed the steps of this tutorial correctly and if your machine does not have another security configuration hindering you, you should now be able to access your application from "mydomain.com" (or whatever your domain may be).

6. Serve your application securely with a TLS certificate (strongly recommended)

Thanks to Nginx as a reverse proxy, you can easily serve your applications securely via a TLS connection. To achieve this, we only need to modify the Nginx "virtual host" configuration file:

server {
    listen 80;
    # Redirects to a secure connection.
    return 301 https://$host$request_uri;
}

server {

    listen 443;
    # Here you should enter your domain name.
    server_name mydomain.com;

    # Here you should specify the path to your SSL certificate
    ssl_certificate           /etc/path/to/certificate/cert.crt;
    ssl_certificate_key       /etc/path/to/certificate/cert.key;

    ssl on;
    ssl_session_cache  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;

    ssl_prefer_server_ciphers on;
    ssl_session_timeout  10m;

    access_log            /var/log/nginx/mydomain.com.access.log;

    location / {
    
      proxy_pass          http://localhost:4000;
    }
  }

Finally, save the changes, restart nginx with:

systemctl reload nginx

and run the application again, if you do not yet have it as a service or domain.

success You're done! If you followed the steps correctly, you now have a web application served through a reverse proxy by nginx.

Remember that if you do not yet have a Cloud server with a Linux operating system, you can easily deploy one with SW Hosting.