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:
wgetandnano.
Installing Nginx Mainline Version
To use the latest Nginx features, install the mainline version.
steps
Download the Nginx signing key:
bashwget https://nginx.org/keys/nginx_signing.keyAdd the signing key:
bashsudo apt-key add nginx_signing.keyAdd the Nginx repository:
bashsudo nano /etc/apt/sources.list.d/nginx.listAdd the following line (replace
focalwith your Ubuntu codename, if different):deb http://nginx.org/packages/mainline/ubuntu/ focal nginx deb-src http://nginx.org/packages/mainline/ubuntu/ focal nginxSave and exit the file (
CTRL+O,Enter,CTRL+X).Update the repository index:
bashsudo apt updateInstall Nginx:
bashsudo apt install nginxVerify Nginx installation:
bashsystemctl status nginx # Check if the service is activeIf inactive, start and enable it:
bashsudo systemctl start nginx sudo systemctl enable nginxCheck 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
Navigate to the Nginx configuration directory:
bashcd /etc/nginx/conf.d/Create a new configuration file for your application:
bashsudo nano nodeapp.confAdd 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; } }Disable the default configuration file:
bashsudo mv default.conf default.conf.disabledTest Nginx configuration for syntax errors:
bashsudo nginx -tReload Nginx to apply changes:
bashsudo nginx -s reload
Testing the Reverse Proxy Setup
Verify that the reverse proxy works correctly.
steps
Start your Node.js application (port 3000 in this example):
bashnode app.jsOpen 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.
If the proxy doesn't work as expected, check the logs:
bashsudo 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.