How To Configure Nginx as a Reverse Proxy on Ubuntu 22.04
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.
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:
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:
sudo nano /etc/nginx/sites-available/your_domain
Insert the following configuration, replacing your_domain
and app_server_address
:
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:
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:
sudo apt update
sudo apt install gunicorn
nano test.py
Insert the following code into test.py
:
def app(environ, start_response):
start_response("200 OK", [])
return iter([b"Hello, World!"])
Run the server:
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.