Server

How to Configure Nginx Reverse Proxy for Nodejs on Centos

How to Configure Nginx Reverse Proxy using Nodejs on Centos-Web Panel 2019

Configure Nginx Reverse Proxy for Nodejs on Centos:

A reverse proxy creates an intermediate layer between the client-side request and passes it to other servers. With the emergence of NodeJs, it is very common to use this with Nginx reverse proxy. In this article, we will show you how to setup Nginx reverse proxy for nodejs on centos panel.

Nodejs is a free open source, lightweight, adaptable and effective JavaScript structure based on Chrome’s V8 JavaScript motor, and uses an occasion driven, non-blocking I/O model. Nodejs is presently all over the place and has turned out to be so famous for creating programming from sites, web applications to arrange applications and the sky is the limit from there.

Nginx is an open source, superior HTTP server, load balancer and reverse proxy. It has a direct setup language making it simple to design. In this article, we will tell the best way to design Nginx as an reverse proxy for Nodejs applications.

CentOS Web Panel – a Free Web Hosting control panel designed for quick and easy management of (Dedicated & VPS) servers minus the chore and effort to use ssh console for every time you want to do something, offers a huge number of options and features for server management in its control panel package.

1.Installing NodeJs on CentOS 7/6

This is the official repository of Nodejs, the latest nodejs and npm packages can be downloaded from here. It is always advised that you always download a stable version of any package. But you can download any version of your choice.

---------- Install Node.js v11.x ---------- 
$ curl -sL https://rpm.nodesource.com/setup_11.x 

---------- Install Node.js v10.x ----------
$ curl -sL https://rpm.nodesource.com/setup_10.x

After downloading now install Nodejs

sudo yum install nodejs -y

Check your Nodejs version

 node -v

Check your npm version

 npm -v

2. Creating a Nodejs Application

We will be creating an app called  “techrado”  and We will be assigning a port number 3000 to it.

$ cd /home/username/public_html/
$ nano app.js

Copy and paste the following code in the app.js file (replace IP_Address with your server IP).

const http = require('http');

const hostname = 'IP_Address';
const port = 3000;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('Techrado App is Up and Running!\n');
});

server.listen(port, hostname, () => {
  	console.log(`Server running at http://${hostname}:${port}/`);
});

Ctrl+O and Ctrl+X

$ sudo node app.js

Now open a browser and access your application at the URL http://Ip_Address:3000.

3.Nginx Installation on CentOS

If you have installed Nginx you can check its version like this.

$ nginx -v

Otherwise, installing Nginx on Centos if it has not been installed on your server

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/version/$basearch/ gpgcheck=0 enabled=1

NOTE: Don’t forget to replace version with your Centos Version.

After successfully installing Nginx, start it, enable it to auto-start at system boot and check if it is up and running.

# wget --quiet http://nginx.org/keys/nginx_signing.key && rpm --import nginx_signing.key
# yum install nginx
 On CentOS  
# systemctl status nginx
# systemctl enable nginx
# systemctl status nginx

4. Configure Nginx as Reverse Proxy For Nodejs Application

Now go the location using cd /etc/nginx/conf.d/ as shown.

$ sudo nano /etc/nginx/conf.d/techrado.conf 

Copy and paste the following configuration (change IP_Address with your server IP and techrado.conf with your domain name).

server {
    listen 80;
    server_name your_domain;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://IP_Address:3000;
    }
}
}

Now press Ctrl+O and Ctrl+X.

Finally, restart the Nginx service to effect the recent changes.

$ systemctl restart nginx

5. Access Nodejs Application through Browser

Now you should be able to access your Node app without providing the port it is listening on, in the URL: this is a much convenient way for users to access it.

http://yourdomain/

What Errors Occur During The Configuration of Nginx Reverse Proxy for Nodejs on Centos?

While configuration for NodeJs on Centos, Everyone faces few errors, some errors are as below.

1. Check your DNS configuration.
2.Remove multiple configuration file if they are activated like vhost.conf,ipaddress.conf,default.conf
3. Setting your hostname
4. Clear your browser cache or put ‘?’ to the end of the URL.

If you have any question feel free to ask in the comment box.

Read Also: How to Setup Centos on Webserver

Add Comment

Click here to post a comment

Featured