Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Nodejs»How to Setup Nginx as Frontend Server for Node.js

    How to Setup Nginx as Frontend Server for Node.js

    By RahulJune 3, 20213 Mins Read

    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.

    Advertisement

    Setup Nginx as Fronted Server for Node.js

    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:

    nginx-with-nodejs-1

    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/ .

    nginx-with-nodejs-2

    javascript nginx NodeJs webserver
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How To Set Up Nginx Reverse Proxy: A Step-By-Step Tutorial

    Configuring the Nginx Reverse Proxy in Front of Apache

    Configure CORS in NGINX

    How to Enable CORS in Nginx

    View 7 Comments

    7 Comments

    1. rj on January 16, 2020 4:17 pm

      in doing this on ubuntu, the browser resolves the actual example.com domain (and not the node.js app)…what am I missing. Thx

      Reply
      • Jarvis Roach on February 9, 2020 6:05 pm

        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
        .
        .
        .
        “

        Reply
    2. Kaerali on January 8, 2020 6:10 pm

      It works perfectly. I thought it would be harder than it actually is. Thanks!

      Reply
    3. Ashish Chauhan on November 25, 2019 6:41 am

      iT IS NOT WORKING,

      Reply
      • Rahul on November 25, 2019 10:08 am

        Hi Ashish, What issue are you facing?

        I have updated the tutorial, hope this will work for you.

        Reply
    4. Keitel Jovin on September 27, 2019 3:11 am

      Thanks for this tuto, it helped me a lot configuring it in a server

      Reply
    5. manikumar on May 6, 2019 8:26 am

      it is not working

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.