Nginx is powerful and widely used for the webserver. It also used as a frontend proxy server for multiple web application servers runs behind this. This tutorial will help you to set up your Nginx server as a frontend proxy server for your Node.js application.
Step 1 – Prerequsities
We are assuming you have pre-installed Node.js on your system. But still, if you want to install Node.js follow this tutorial.
Step 2 – Create Sample Node Application
As you are here 🙂 You must be running your Node application on some port. We assume your application is running on port 3000. So for the demonstration, I am creating a sample web application on Node.js and run on port 3000. So it will be easier to understand for you.
If you don’t have running application, You can also follow below instruction to run a sample web application. So, create a JavaScript file and edit in your favorite text editor.
vi myapp.js
Then, add the following content in the javascript file.
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World'); }).listen(3000, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/');
Your Node application is ready to serve on port 3000. Let’s start the Node.js application in the background.
node myapp.js &
And access this in the browser. You will see the result like below:
Step 3 – Install Nginx
Now install the Nginx web server using the default Package manager. The Ubuntu and Debian based systems use apt, Fedora and CentOS/RHEL 8 use DNF and CentOS/RHEL 7/6 uses yum. Nginx is available under default repositories on almost operating systems.
sudo apt install nginx
### Debians based systems sudo yum install nginx
### CentOS 7/6 sudo dnf install nginx
### Fedora & CentOS 8
Step 4 – Configure NGINX
After starting a demo server with node.js. Now start configuration with Nginx. Create a virtual host configuration file for your domain under /etc/nginx/conf.d/ directory.
sudo vim /etc/nginx/conf.d/example.com.conf
and add the following content.
#Setup upstream for backend Node.js server upstream myapp { server 127.0.0.1:3000; keepalive 8; } #The Nginx server instance server { listen 0.0.0.0:80; server_name example.com www.example.com; access_log /var/log/nginx/example.com.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://myapp/; proxy_redirect off; } }
After creating the configuration, restart Nginx web server using following command.
sudo systemctl restart nginx
Step 5 – Verify Setup
Now access your server using domain name, you will see the same page shows on http://127.0.0.1:3000/ .
7 Comments
in doing this on ubuntu, the browser resolves the actual example.com domain (and not the node.js app)…what am I missing. Thx
RJ,
I was having the same issue. I resolved it by adding “10.0.2.15 example.com” to my /etc/hosts files, where 10.0.2.15 is the IP address of my Linux VM. Hope this helps.
”
127.0.0.1 localhost
127.0.1.1 minty
10.0.2.15 example.com
.
.
.
“
It works perfectly. I thought it would be harder than it actually is. Thanks!
iT IS NOT WORKING,
Hi Ashish, What issue are you facing?
I have updated the tutorial, hope this will work for you.
Thanks for this tuto, it helped me a lot configuring it in a server
it is not working