Tutorials  /  Ubuntu

How To Configure Nginx as a Reverse Proxy on Ubuntu 22.04

Ccentron Redaktion · December 2024 ·4 min read ·Ubuntu, Tutorial

Introduction

A reverse proxy is the recommended method to expose an application server to the internet. Whether you are running a Node.js application in production or a minimal built-in web server with Flask, these application servers will often bind to localhost with a TCP port. This means your application will only be accessible locally on the machine it resides on. Using a reverse proxy in production provides security benefits, centralized firewall protection, and a minimized attack plane for common threats like denial of service attacks.

This tutorial demonstrates how to set up a reverse proxy using Nginx. You will install Nginx, configure it with the proxy_pass directive, and forward headers from client requests. Optionally, you will set up a test application with the WSGI server Gunicorn.

VM

Matching infrastructure at centron

Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Prerequisites for Reverse Proxy on Ubuntu

To complete this tutorial, you will need:

  • An Ubuntu 22.04 server with a non-root sudo-enabled user and a firewall. Follow our initial server setup guide.
  • The address of the application server you want to proxy, referred to as app_server_address. This could be an IP address with a TCP port or a Unix domain socket.
  • A domain name pointed at your server’s public IP, configured with Nginx to proxy your application server.

Step 1 — Installing Nginx

Nginx can be installed with apt through the default repositories. Run the following commands:

Code

sudo apt update
sudo apt install nginx
sudo ufw allow 'Nginx HTTP'
systemctl status nginx
  

Ensure that Nginx is running and accessible through the firewall.

Step 2 — Configuring your Server Block

Create a custom configuration file for your new server block additions:

Code

sudo nano /etc/nginx/sites-available/your_domain
  

Insert the following configuration, replacing your_domain and app_server_address:

Code

server {
    listen 80;
    listen [::]:80;

    server_name your_domain www.your_domain;

    location / {
        proxy_pass app_server_address;
        include proxy_params;
    }
}
  

Save and exit, then enable the configuration:

Code

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
  

Step 3 — Testing your Reverse Proxy on Ubuntu with Gunicorn (Optional)

If you don’t have an application server, set up Gunicorn for testing:

Code

sudo apt update
sudo apt install gunicorn
nano test.py
  

Insert the following code into test.py:

Python

def app(environ, start_response):
    start_response("200 OK", [])
    return iter([b"Hello, World!"])
  

Run the server:

Code

gunicorn --workers=2 test:app
  

Navigate to your_domain in your browser to test the reverse proxy.

Conclusion for How To Configure Nginx as a Reverse Proxy on Ubuntu

With this tutorial, you have configured Nginx as a reverse proxy to enable access to your application servers that would otherwise only be accessible locally. Additionally, you configured the forwarding of request headers. For more examples, check out tutorials on serving Flask applications with Gunicorn and Nginx or running a Meilisearch frontend with InstantSearch on Ubuntu 22.04.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Ubuntu
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren