Node.js + Nginx - What now?

后端 未结 12 1620
抹茶落季
抹茶落季 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:51

    I made a repository in Github which you can clone, vagrant-node-nginx-boilerplate

    basically the node.js app at /var/www/nodeapp is

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(4570, '127.0.0.1');
    
    console.log('Node Server running at 127.0.0.1:4570/');
    

    and the nginx config at /etc/nginx/sites-available/ is

    server {
            listen 80 default_server;
            listen [::]:80 default_server;
    
            root /var/www/nodeapp;
            index index.html index.htm;
    
            server_name localhost;
    
            location / {
              proxy_pass http://127.0.0.1:4570;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_cache_bypass $http_upgrade;
            }
    }
    

提交回复
热议问题