Iquidus Explorer – Reverse Proxy Port 3001 to 80

114 Likes Comment

Redirecting traffic from one port to another

Let’s issue the following command to redirect incoming traffic on port 80 to our Node.js app that is listening on port 1337:

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3001

Automatically re-apply the rule on system reboot

Next, go ahead and install iptables-persistent to save the rule permanently and have it reapplied automatically when your system reboots:

sudo apt-get install iptables-persistent
sudo /etc/init.d/iptables-persistent save 

That’s it. Now your Node.js app is accessible on port 80 and you should be good to go.

Installing Nginx

sudo apt-get install nginx

Configuring Nginx

Next, we’ll need to configure Nginx so that it forwards traffic to our app. Let’s start off by removing the default configuration file:

sudo rm /etc/nginx/sites-enabled/default

Next, create a new file in /etc/nginx/sites-available/ called node and open it with nano:

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

Paste the following code in the file and make sure to change example.com to your domain (or IP), and 3001 to your Node.js application port:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:3001";
    }
}

The proxy_pass declaration configures Nginx to act as a reverse proxy by forwarding all incoming requests on port 80 to your Node.js app on port 1337, on behalf of the client.

Next, we need to symlink our configuration to sites-enabled for it to be used by Nginx, since it’s currently in sites-available:

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

Applying the Configuration

Let’s restart Nginx so that it loads our configuration:

sudo service nginx restart

You might like

Avatar

About the Author: Toc Xoan

Leave a Reply

Your email address will not be published. Required fields are marked *