Linking a Node.js Application to Port 80 with Nginx
You’ve finished building your first Node.js web app and, man, it’s awesome. Everything works fine on your development machine, but when you try to deploy to production, your app just doesn’t bind to the desired port. This is because Linux restricts applications from binding to the first 1024 ports unless they are given root permissions to prevent malicious processes from binding to sensitive ports such as 80, 443, 22, 21, etc.
Here are some ways to deal with this security restriction:
IPTABLES
One way is to set up an iptables forwarding rule. iptables will then bind to the desired port (80 or 443) and forward all incoming traffic to your application’s port, such as 1337.
However, this solution requires you to persist the iptablesrule so that it runs at system startup, because your machine will crash or reboot eventually.
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 1337
Automatically reapply rule on system restart.
Then go ahead and install iptables-persistent to save the rule permanently and automatically reapply when the system reboots:
sudo apt-get install iptables-persistent
sudo /etc/init.d/iptables-persistent save
That’s it. Your Node.js app is now accessible on port 80 and you should be ready to go.
NGINX
A more popular approach is to configure Nginx as a reverse proxy by making it bind to the desired port, forwarding all incoming traffic to your Node.js application.
Nginx is a high-performance open source web server (similar to Apache) that is widely used as a reverse proxy for Node.js applications.
The main benefit of Nginx is that it takes care of transport optimization. It sends cache headers for static resources and compresses them so your Node.js app doesn’t have to deal with these things. Focus on building your product and let Nginx handle the optimizations.
Installing Nginx
Easy as pie.
sudo apt-get install nginx
Configuring Nginx
Next, we’ll need to configure Nginx to forward traffic to our app. Let’s start by removing the default configuration file:
sudo rm /etc/nginx/sites-enabled/default
Then create a new file in /etc/nginx/sites-available/named node and open it with nano:
sudo nano /etc/nginx/sites-available/bdcash
Paste the following code into the file and make sure to switch to your domain (or IP) and application port Node.js.example.com1337
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:1337";
}
}
The proxy_pass statement configures Nginx to act as a reverse proxy, forwarding all incoming requests on port 80 to its Node.js application on port 1337, on behalf of the client.
Next, we need to create a symlink for our sites-enabled setting to be used by Nginx since it’s currently in sites-available:
sudo ln -s /etc/nginx/sites-available/bdcash /etc/nginx/sites-enabled/bdcash
Applying the Configuration
Let’s restart Nginx so that it loads our configuration:
sudo service nginx restart
All ready! Nginx will now forward all incoming requests to your app and will even survive a server crash as it automatically boots with your machine.