Node.js + Nginx - 现在怎么办? [英] Node.js + Nginx - What now?

查看:30
本文介绍了Node.js + Nginx - 现在怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的服务器上设置了 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. 他们应该如何合作?我应该如何处理这些请求?
  2. Node.js 服务器有两个概念,哪个更好:

  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:

一个.为每个需要它的网站创建一个单独的 HTTP 服务器.然后在程序开始时加载所有的 JavaScript 代码,这样代码就被解释了一次.

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.创建一个单独的 Node.js 服务器来处理所有 Node.js 请求.这将读取请求的文件并评估它们的内容.因此,每个请求都会解释文件,但服务器逻辑要简单得多.

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.

我不清楚如何正确使用 Node.js.

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

推荐答案

Nginx 作为前端服务器工作,在这种情况下,它将请求代理到 node.js 服务器.因此,您需要为节点设置一个 nginx 配置文件.

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.

这是我在我的 Ubuntu 盒子中所做的:

This is what I have done in my Ubuntu box:

/etc/nginx/sites-available/ 创建文件 yourdomain.com:

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;
    }
 }

如果您还希望 nginx (>= 1.3.13) 处理 websocket 请求,请在 location/ 部分添加以下几行:

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

/var/www/yourdomain/app.js 创建您的节点服务器应用程序并在 localhost:3000

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
');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

测试语法错误:

nginx -t

重启nginx:

sudo /etc/init.d/nginx restart

最后启动节点服务器:

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

现在您应该会在 yourdomain.com 看到Hello World"

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

关于启动节点服务器的最后一个注意事项:您应该为节点守护程序使用某种监视系统.有一个很棒的关于带有 upstart 和 monit 的节点的教程.

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.

这篇关于Node.js + Nginx - 现在怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆