Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Nginx»How to Setup Nginx as Frontend Server for Node.js

    How to Setup Nginx as Frontend Server for Node.js

    RahulBy RahulJanuary 8, 20163 Mins ReadUpdated:June 3, 2021

    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.

    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
    Previous ArticleHow To Install RabbitVCS on Ubuntu 15.10, 14.04 & 12.04
    Next Article How to Install PostgreSQL on Ubuntu 18.04 & 16.04 LTS

    Related Posts

    How To Install NVM on Ubuntu 22.04

    Updated:April 16, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How To Install NVM on Windows

    Updated:April 16, 20223 Mins Read

    How to Increase Request Timeout in NGINX

    Updated:January 13, 20222 Mins Read

    How To Install Node.Js on Debian 11

    Updated:February 11, 20226 Mins Read

    How To Install NVM on Debian 11

    Updated:August 31, 20213 Mins Read

    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

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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