Coder Social home page Coder Social logo

node-nginx's Introduction

Configure nginx as a web server for node js application.

Requirements

nodejs npm nginx

Steps to run on your local machine

  • Clone the repo
  • npm i
  • start nginx
  • head to localhost and check if your nginx is up and running
  • Add these contents to the nginx conf file (change the port number, server name in accordance)
    upstream my_http_servers {
        least_conn;
        server 127.0.0.1:3001;      # httpServer1 listens to port 3001
        server 127.0.0.1:3002;      # httpServer2 listens to port 3002
        server 127.0.0.1:3003;      # httpServer2 listens to port 3003
        server 127.0.0.1:3004;      # httpServer2 listens to port 3004
    }
    
    server {
        listen       81;
        server_name  localhost;
    
        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://my_http_servers;
    
            proxy_redirect off;
        }
    }
    

    Run your node application in different ports

    pm2 start app.js --node-args="app.js 3001" --name firstInstance

    pm2 start app.js --node-args="app.js 3002" --name secondInstance

    pm2 start app.js --node-args="app.js 3003" --name thirdInstance

    pm2 start app.js --node-args="app.js 3004" --name fourthInstance


  • Reload your nginx (nginx -s reload)
  • Go to browser and navigate to localhost:81 to see which port the application runs on
  • Keep reloading the browser to confirm whether it runs on multiple ports
  • You can also check the application logs by running pm2 logs.

    Screenshots

    Dashboard dashboard

    pm2 monitor monitor

    pm2 logs

    logs

    pm2 list list

  • node-nginx's People

    Contributors

    ashwin1111 avatar

    Watchers

    James Cloos avatar  avatar

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.