DEV Community

Cover image for Configuring Nginx as a Reverse Proxy on Ubuntu 22.04
Mouhamadou Tidiane Seck
Mouhamadou Tidiane Seck

Posted on

Configuring Nginx as a Reverse Proxy on Ubuntu 22.04

A reverse proxy is a server that sits between client devices and backend servers, forwarding client requests to the appropriate server. Nginx is commonly used as a reverse proxy due to its stability, simple configuration, and low resource consumption. In this guide, we'll walk through how to configure Nginx as a reverse proxy.

Network Architecture

A reverse proxy typically has at least two network interfaces:

  1. External Interface: This interface is exposed to the internet and handles requests from external users.
  2. Internal Interface: This interface communicates with the backend web servers that hold the requested information.

Here’s a diagram illustrating the basic flow:

External Users -> Reverse Proxy (Nginx) -> Internal Web Servers
Enter fullscreen mode Exit fullscreen mode

Roles and Benefits of a Reverse Proxy

Using a reverse proxy like Nginx offers several advantages:

  • Load Balancing: Distributes incoming traffic across multiple web servers, improving performance and availability.
  • Traffic Management: Allows you to manage server maintenance or migrations without downtime.
  • Centralized Configuration: Manages access and configuration for multiple sites and web servers from a single location.
  • Traffic Optimization: Uses caching and compression to improve response times.
  • Enhanced Security: Handles SSL encryption and filters requests/URLs to increase security.

Configuring Nginx as a Reverse Proxy

Step 1 — Installing Nginx

Start by installing Nginx on your server:

  • Update the package index:
  sudo apt update
Enter fullscreen mode Exit fullscreen mode
  • Install Nginx:
  sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  • Ensure Nginx is running:
  systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Step 2 — Configuring the Firewall (Optional)

If your server’s firewall is active, you'll need to allow HTTP traffic:

  • Allow HTTP traffic:
  sudo ufw allow 'Nginx HTTP'
Enter fullscreen mode Exit fullscreen mode

Step 3 — Creating a Configuration File

Create a configuration file for your site:

  • Use a text editor like nano to create a new configuration file in the /etc/nginx/sites-available/ directory:
  sudo nano /etc/nginx/sites-available/your_domain
Enter fullscreen mode Exit fullscreen mode

Step 4 — Configuring the Server Block

Edit the server block configuration with your domain and application server address:

  • Insert the following configuration into your new file, replacing your_domain and app_server_address with your actual domain and server address:
  server {
      listen 80;
      listen [::]:80;

      server_name your_domain www.your_domain;

      location / {
          proxy_pass http://app_server_address;
          include proxy_params;
      }
  }
Enter fullscreen mode Exit fullscreen mode

Step 5 — Activating the Configuration

Activate the new configuration file:

  • Create a symbolic link from the configuration file to the directory that Nginx reads from at startup:
  sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Step 6 — Testing the Configuration

Test the Nginx configuration for syntax errors and then restart Nginx:

  • Verify the configuration:
  sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  • Restart Nginx to apply the changes:
  sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Optional: Using Gunicorn

If you need to run a Python web application, you can use Gunicorn as an application server:

  • Install Gunicorn:
  sudo apt install gunicorn
Enter fullscreen mode Exit fullscreen mode
  • Create a test application that returns "Hello World!" in an HTTP response to verify Gunicorn is working correctly.

Conclusion

You have successfully configured Nginx as a reverse proxy for your websites. This setup not only optimizes traffic management but also enhances the security and scalability of your web infrastructure. Feel free to explore more Nginx features to further improve your deployment!


Top comments (0)