必承其重 | 欲带皇冠 2011-02-15 20:49 采纳率: 0%
浏览 405
已采纳

3. Node.js + Nginx-现在怎么办?

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 handle the requests?
  2. There are 2 concepts for a Node.js server, which one is better:

    a. Create a separate HTTP server for each website that needs it. Then load all JavaScript code at the start of the program, so the code is interpreted once.

    b. Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.

It's not clear for me how to use Node.js correctly.

转载于:https://stackoverflow.com/questions/5009324/node-js-nginx-what-now

  • 写回答

11条回答 默认 最新

  • ℙℕℤℝ 2011-02-16 10:20
    关注

    Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to setup an nginx config file for node.

    This is what I have done in my Ubuntu box:

    Create the file yourdomain.com at /etc/nginx/sites-available/:

    vim /etc/nginx/sites-available/yourdomain.com
    

    In it you should have something like:

    # the IP(s) on which your node server is running. I chose port 3000.
    upstream app_yourdomain {
        server 127.0.0.1:3000;
        keepalive 8;
    }
    
    # the nginx server instance
    server {
        listen 80;
        listen [::]:80;
        server_name yourdomain.com www.yourdomain.com;
        access_log /var/log/nginx/yourdomain.com.log;
    
        # pass the request to the node.js server with the correct headers
        # and much more can be added, see nginx config options
        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://app_yourdomain/;
          proxy_redirect off;
        }
     }
    

    If you want nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the location / section:

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    

    Once you have this setup you must enable the site defined in the config file above:

    cd /etc/nginx/sites-enabled/ 
    ln -s /etc/nginx/sites-available/yourdomain.com yourdomain.com
    

    Create your node server app at /var/www/yourdomain/app.js and run it at localhost:3000

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

    Test for syntax mistakes:

    nginx -t
    

    Restart nginx:

    sudo /etc/init.d/nginx restart
    

    Lastly start the node server:

    cd /var/www/yourdomain/ && node app.js
    

    Now you should see "Hello World" at yourdomain.com

    One last note with regards to starting the node server: you should use some kind of monitoring system for the node daemon. There is an awesome tutorial on node with upstart and monit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题