NodeJS+Nginx as a reverse proxy
NodeJS is an open source JavaScript runtime environment for easily building server side applicaiton.
Let's setup our application with Nginx as reverse proxy
NodeJS is an open source JavaScript runtime environment for easily building server side applicaiton.
Let's setup our application with Nginx as reverse proxy
Prerequisites-
- Ubuntu 16.04 Machine
- A user with Sudo access
- Nginx Installed
To install Nginx run the following command.
$ sudo apt install nginx
Check Nginx service status
sudo systemctl status nginx.service
Nginx installation completed, Service up and running.
Step 2-Install NodeJS
Install latest NodeJS from GIT repository-
sudo curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
To Install NodeJS run following command.
sudo apt install nodejs
NodeJs installation has done. Check NodeJS Version
$ sudo nodejs --version
v9.10.0
Node Version 9.10.0 has installed.
Step 3-Create Node.js Application -
We will create a simple node.js application to our test purpose.
Create app directoy and go to app directory
$ sudo mkdir -p /var/www/myapp && cd /var/www/myapp
Create sample node.js application
$ sudo vi myapp.js
Now appnend follwoing lines
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to Linux How to Guide Blog World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
In the above syntax nodejs app listting on localhsot, Save and Exit from the file.
Node.JS simple applicaiton will work on localhost and will listens port 8080
Step 4- Test application
In order to test application, make it executalble.
sudo chmod a+x myapp.js
Run the myapp.js file.
amar@ubuntu16:/var/www/myapp$ sudo ./myapp.js
Server running at http://localshost:8080/
You will get output - Server running at http://localhost:8080/
Let's browse URL - http://localhost:8080/
Node.JS application is ready and working as expected.
Step 5- Keep running our application in the backgroud.
Node.JS application will work until you terminal is active, let's keep it running in the background.
$ sudo nohup ./myapp.js &
Step 6- Set Up Nginx as a reverse proxy server
Now that your application is running, and listening on ServerIP, you need to set up a way for your users to access it. We will set up the Nginx web server as a reverse proxy for this purpose.
Modify Nginx default Sever block -
$ sudo vi /etc/nginx/sites-available/default
Append following line-
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
Now your Server block should be like this-
Save and exit from the file.
Check Ngnix configuration file:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx Service
sudo systemctl restart nginx.service
Step 7- Test Application access with server IP address
All the required configuration made successfully, Now Nginx proxy is ready to work with Nodejs application.
Open your browser and access URL- http://192.168.102.10
Nginx configuration as reverse proxy completed successfully.
Nice and good article, useful for everyone.
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
Thanks for such a great article here.
ReplyDeleteMern stack online training
Mern stack training in hyderabad
This is really helpful and informative, as this gave me more insight to make more ideas and solutions.keep update with your blog post.
ReplyDeleteMean stack online training
Mean stack training in hyderabad