Developer Guides

How to Configure an Nginx Reverse Proxy

Learn how to set up an Nginx reverse proxy to expose your localhost applications securely to the public internet.

3 min read

Learn how to set up an Nginx reverse proxy to expose your localhost applications securely to the public internet. This guide walks you through installing Nginx, configuring it as a reverse proxy, and testing your setup.

Prerequisites for Configuring Nginx Reverse Proxy

Before you begin, ensure you have the following:

prerequisites

  • A server running Ubuntu 20.04 (or similar).
  • SSH access to the server.
  • A Node.js application already running on localhost (port 3000 in this guide).
  • Installed tools: wget and nano.

Installing Nginx Mainline Version

To use the latest Nginx features, install the mainline version.

steps

  1. Download the Nginx signing key:

    bash
    wget https://nginx.org/keys/nginx_signing.key
  2. Add the signing key:

    bash
    sudo apt-key add nginx_signing.key
  3. Add the Nginx repository:

    bash
    sudo nano /etc/apt/sources.list.d/nginx.list

    Add the following line (replace focal with your Ubuntu codename, if different):

    deb http://nginx.org/packages/mainline/ubuntu/ focal nginx
    deb-src http://nginx.org/packages/mainline/ubuntu/ focal nginx

    Save and exit the file (CTRL+O, Enter, CTRL+X).

  4. Update the repository index:

    bash
    sudo apt update
  5. Install Nginx:

    bash
    sudo apt install nginx
  6. Verify Nginx installation:

    bash
    systemctl status nginx # Check if the service is active

    If inactive, start and enable it:

    bash
    sudo systemctl start nginx
    sudo systemctl enable nginx

    Check in a browser via your server's public IP address or domain. You should see the Welcome to Nginx page.

Configuring Nginx as a Reverse Proxy

Follow these steps to set up Nginx as a reverse proxy for your application.

steps

  1. Navigate to the Nginx configuration directory:

    bash
    cd /etc/nginx/conf.d/
  2. Create a new configuration file for your application:

    bash
    sudo nano nodeapp.conf
  3. Add the following configuration (replace <your-ip-or-domain>):

    server {
        listen 80;
        server_name <your-ip-or-domain>;
    
        location / {
            proxy_pass http://localhost:3000;
            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;
        }
    }
  4. Disable the default configuration file:

    bash
    sudo mv default.conf default.conf.disabled
  5. Test Nginx configuration for syntax errors:

    bash
    sudo nginx -t
  6. Reload Nginx to apply changes:

    bash
    sudo nginx -s reload

Testing the Reverse Proxy Setup

Verify that the reverse proxy works correctly.

steps

  1. Start your Node.js application (port 3000 in this example):

    bash
    node app.js
  2. Open a browser and visit your server's public IP or domain (without port 3000). The Nginx reverse proxy should forward the request to your application, and you should see the app content.

  3. If the proxy doesn't work as expected, check the logs:

    bash
    sudo tail -f /var/log/nginx/error.log

Advantages and Next Steps

With Nginx as a reverse proxy, your application is now securely exposed to the public internet. This setup offers numerous benefits:

FAQ

Why use an Nginx reverse proxy?

An Nginx reverse proxy enhances your application's security and scalability by routing requests, offloading SSL, managing load balancing, and enabling session handling.

What is nginx -t?

nginx -t checks the syntax of your Nginx configuration files. Always run this before reloading Nginx to avoid service interruptions from configuration errors.

Can I use other tools instead of Nginx?

Yes, you can use alternative tools like Caddy or Traefik. These may be simpler to configure, but Nginx offers extensive flexibility and a robust feature set, making it a popular choice for more advanced setups.


Official reference: nginx documentation.