Skip to content

Web serverFor beginnersDuration around 20 min.

How to set up Nginx as a reverse proxy with HTTPS (Let’s Encrypt)

Following this tutorial, Nginx will accept requests to your domain, pass them to an application on the same server, and serve everything over HTTPS with a Let’s Encrypt certificate. This is suitable for Node.js, Python, Go, or a container listening on a local port.

Tested on with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS

At a glance

  • Nginx listens on ports 80 and 443 and passes every request to the application on 127.0.0.1:3000 — the application itself is not accessible from the outside.
  • Four headers tell the application which domain, which address, and which protocol the visitor used; two more keep WebSockets open.
  • Certbot fetches the certificate, adds it to the configuration, and renews it automatically before it expires.

Prerequisites

  • A server with Ubuntu 24.04 LTS or 26.04 LTS and a user with sudo privileges — such as a VPS or a Cloud Server.
  • A domain whose A record (and AAAA record for IPv6) points to the server's address. In the examples, it is called app.example.com; replace the name with yours throughout.
  • An application that responds on 127.0.0.1:3000 on the server. If it runs in a container, bind the port to 127.0.0.1 — as shown in the Install Docker on Ubuntu tutorial.

Step 1: Install Nginx

Install Nginx
sudo apt update
sudo apt install -y nginx

Nginx starts immediately after installation. Opening the server address in a browser shows the “Welcome to nginx!” welcome page.

Step 2: Prepare WebSockets

Applications with live features keep a WebSocket connection open. For this, Nginx must set the Connection header depending on the request. This is handled by a map block that applies once to all sites:

/etc/nginx/conf.d/websocket.conf
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

Step 3: Create a server block for the application

The server block accepts requests for your domain and passes them to the application. Host specifies the requested domain, X-Real-IP and X-Forwarded-For the visitor's address, and X-Forwarded-Proto whether they used HTTP or HTTPS.

/etc/nginx/sites-available/app.example.com
server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;
 
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

You create both files with an editor, such as nano:

Create files
sudo nano /etc/nginx/conf.d/websocket.conf
sudo nano /etc/nginx/sites-available/app.example.com

Step 4: Enable and test the site

A link in sites-enabled enables the site. nginx -t tests the entire configuration before Nginx reloads it — this ensures a typo never brings down the running server.

Enable, test, reload
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Output of nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

A request on the server itself shows whether the forwarding works. The response comes from your application, not from the welcome page.

Test forwarding
curl -H "Host: app.example.com" http://127.0.0.1/

Step 5: Open the firewall

The Nginx package includes profiles for ufw: “Nginx HTTP” (port 80), “Nginx HTTPS” (443), and “Nginx Full” (both). Allow SSH before enabling ufw — otherwise, you will lock out your own connection.

Set up a firewall with ufw
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

Do not open port 3000 of the application: Nginx reaches it via 127.0.0.1.

Step 6: HTTPS with Certbot and Let’s Encrypt

Install Certbot as a snap, as recommended by the project; snapd is pre-installed on Ubuntu Server. The third command asks for an email address and your agreement to the Let’s Encrypt terms on the first run, verifies via port 80 that the domain points to this server, and adds the certificate to the server block.

Install Certbot and fetch the certificate
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
sudo certbot --nginx -d app.example.com

Certbot sets up the renewal itself. A dry run, which changes nothing, tests whether it will work:

Test renewal
sudo certbot renew --dry-run

What Certbot changes in the file

Certbot adds listen 443 ssl and the paths to the certificate to the server block. Your location with the headers remains as it is. Test again afterwards with sudo nginx -t.

Typical errors

  • 502 Bad Gateway: Nginx cannot reach the application — it is not running or is listening on a different port. The error log will then show “connect() failed (111: Connection refused) while connecting to upstream”.
  • The welcome page appears instead of the application: The name in server_name does not match the requested domain, or the link in sites-enabled is missing.
  • The application only sees 127.0.0.1 as the visitor: It does not evaluate X-Forwarded-For. Many frameworks require a setting like “trust proxy” for this.
  • 413 Request Entity Too Large when uploading: By default, Nginx accepts a maximum of 1 MB per request. Set something like client_max_body_size 50m; in the server block.
  • Certbot fails the domain validation: The DNS record does not point to the server yet, or port 80 is blocked.
Read error log
sudo tail -n 20 /var/log/nginx/error.log

We tested all commands in this guide on 2 December 2026 — with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about Nginx as a reverse proxy

What is a reverse proxy?

A reverse proxy accepts requests from the internet and passes them on to one or more services behind it. It bundles HTTPS, domains and ports in one place, so that the application itself only needs to be accessible internally.

Can Nginx serve multiple applications on one server?

Yes. Each application gets its own server block with its own server_name and its own port in proxy_pass. Nginx distinguishes the requests by the requested domain.

Why do I need the map block?

It sets the Connection header to upgrade only if the browser requests a WebSocket connection, and to close otherwise. Without it, live features behind the proxy will break.

Do I have to renew the Let’s Encrypt certificate myself?

No. The Certbot package includes a timer that renews certificates before they expire. The dry run with renew --dry-run shows whether this will work.

Does this also work with containers?

Yes. Publish the container's port to 127.0.0.1 and enter it in proxy_pass. This keeps the container inaccessible from the outside, because Docker bypasses ufw rules for published ports.

Install Docker on Ubuntu

Further reading

More tutorials

A server for your application

VPS include an IPv4 address and an IPv6 /64 — enough for Nginx, HTTPS and the application behind it.