Tutorials  /  Ubuntu

How To Configure Nginx as a Reverse Proxy on Ubuntu 22.04

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

Contents

  1. Introduction
  2. Prerequisites for Reverse Proxy on Ubuntu
  3. Step 1 — Installing Nginx
  4. Step 2 — Configuring your Server Block
  5. Step 3 — Testing your Reverse Proxy on Ubuntu wit…
  6. Conclusion for How To Configure Nginx as a Revers…

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.

OPS

Rather have your server managed?

centron takes care of updates, monitoring and troubleshooting: managed servers from €54.04 per month, run in our own data center in Germany. View managed servers →

Prefer to manage it yourself? Cloud servers from €4.04 per month →

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.

Claim €200 credit

Test your setup on ccloud³

Sign up for ccloud³ and receive €200 starting credit for your project.

centron Redaktion Technical editorial team

The centron editorial team writes guides from day-to-day operations: tested on our own platform, run in our data centre in Hallstadt near Bamberg.

Category Ubuntu
Share
Still have questions?

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

Was this tutorial helpful?

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

Comments

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.

Get started for free

Sign up and receive €200 credit at centron within your first 60 days.

This promotional offer applies to new accounts only. Available exclusively to businesses.