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

查看:97
本文介绍了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:

a.为每个需要它的网站创建一个单独的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\n');
}).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天全站免登陆