NGINX Reverse Proxy Tutorial
A

Lead Engineer @ Packetware

NGINX Reverse Proxy Tutorial

Prerequisites

  • A server (Linux-based, such as Ubuntu or CentOS) with a public IP.

  • Nginx installed on your server. If you haven't installed it yet, you can do so using:

    • Ubuntu: sudo apt update && sudo apt install nginx
    • CentOS: sudo yum install nginx
  • A basic understanding of how to access and edit server files using command line.

Step 1: Basic Nginx Setup

First, ensure that Nginx is running:

sudo systemctl start nginx
sudo systemctl enable nginx

Check the status to confirm:

sudo systemctl status nginx

You should see a message indicating that Nginx is active and running.

Step 2: Configure Nginx as a Reverse Proxy

Default Configuration

By default, Nginx serves files from the /var/www/html directory or elsewhere as specified in its default server block configuration file located in /etc/nginx/sites-available/ on Ubuntu and /etc/nginx/conf.d/ on CentOS.

To set up a reverse proxy, you will edit the configuration file to redirect incoming requests to another server.

Create or Modify Configuration

  1. Edit the configuration file:

    Ubuntu:

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

    CentOS:

    sudo nano /etc/nginx/nginx.conf
    
  2. Configure Reverse Proxy:

Add or modify the existing server block to include the location block with a proxy_pass directive. Here's a basic example assuming you wish to proxy requests to a server running on http://localhost:3000.

server {
    listen 80;

    server_name yourdomain.com;

    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;
    }
}
  • proxy_pass: Specifies the proxied server's URL.
  • proxy_set_header: Ensures that some important headers are preserved for logging, security, and identifying the real client's IP address.

Enable Site Configuration (Ubuntu Specific)

After setting up, ensure your site configuration is enabled:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

Test Configuration

Always test your configuration for errors before reloading Nginx. Run:

sudo nginx -t

If successful, you should see: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok.

Restart Nginx

Apply your new configuration by restarting Nginx:

sudo systemctl restart nginx

Step 3: Set Up DNS (Optional)

Ensure that your domain (e.g., yourdomain.com) points to your server's IP address. This can be done from your domain registrar's DNS settings by adding or modifying an A record.

Conclusion

Your Nginx reverse proxy should now be working. Test it by accessing your domain name in a web browser: http://yourdomain.com. This request should be automatically redirected to the service running on http://localhost:3000.

For additional features such as SSL encryption using Let's Encrypt or advanced load balancing capabilities, consider expanding your configuration using available Nginx modules and tools.