Node.js + Nginx - What now?

后端 未结 12 1615
抹茶落季
抹茶落季 2020-11-22 00:26

I\'ve set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:

  1. How should they work together? How should I handl
12条回答
  •  北海茫月
    2020-11-22 00:39

    We can easily setup a Nodejs app by Nginx acting as a reverse proxy.
    The following configuration assumes the NodeJS application is running on 127.0.0.1:8080,

      server{
         server_name domain.com sub.domain.com; # multiple domains
    
         location /{ 
          proxy_pass http://127.0.0.1:8080;  
          proxy_set_header Host $host;
          proxy_pass_request_headers on;  
         }
    
         location /static/{
           alias /absolute/path/to/static/files; # nginx will handle js/css
         }
       } 
    

    in above setup your Nodejs app will,

    • get HTTP_HOST header where you can apply domain specific logic to serve the response. '
    • Your Application must be managed by a process manager like pm2 or supervisor for handling situations/reusing sockets or resources etc.

    • Setup an error reporting service for getting production errors like sentry or rollbar

    NOTE: you can setup logic for handing domain specific request routes, create a middleware for expressjs application

提交回复
热议问题