我已经在服务器上设置了Node.js和Nginx。现在我想用它,但是在我开始之前有两个问题:

How should they work together? How should I handle the requests? 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.

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


当前回答

带有Nginx配置的Node.js。

$ sudo nano /etc/nginx/sites-available/subdomain.your-domain.example

添加以下配置,这样当我们从subdomain.your_domain.example来的时候,Nginx作为代理将流量重定向到端口3000

upstream subdomain.your-domain.example {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  listen [::]:80;
  server_name subdomain.your-domain.example;
  access_log /var/log/nginx/subdomain.your-domain.access.log;
  error_log /var/log/nginx/subdomain.your-domain.error.log debug;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://subdomain.your-domain.example;
    proxy_redirect off;
  }
}

其他回答

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

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

创建文件yourdomain。例如/etc/nginx/sites-available/:

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

你应该有如下内容:

# 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.example www.yourdomain.example;
    access_log /var/log/nginx/yourdomain.example.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 / section中添加以下行:

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

一旦你有了这个设置,你必须启用上面配置文件中定义的站点:

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

在/var/www/yourdomain/app.js上创建你的节点服务器应用程序,并在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

现在你应该在你的域名中看到“Hello World”

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

回答你的问题2:

我会选择选项b,因为它消耗的资源少得多。使用选项“a”,每个客户端都会导致服务器消耗大量内存,加载所有你需要的文件(即使我喜欢php,这是它的一个问题)。使用选项“b”,您可以加载您的库(可重用代码),并在所有客户端请求之间共享它们。

但是要注意,如果你有多个核,你应该调整node.js来使用所有的核。

如果你想管理并运行每个微服务,你可以使用pm2运行nodejs。节点将运行在一个端口上,只需在Nginx中配置该端口(/etc/ Nginx /sites-enabled/domain.example)

server{
    listen 80;
    server_name domain.example www.domain.example;

  location / {
     return 403;
  }
    location /url {
        proxy_pass http://localhost:51967/info;
    }
}

使用ping检查本地主机是否运行。

And

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.

这是最好的,正如你说的,也更容易

带有Nginx配置的Node.js。

$ sudo nano /etc/nginx/sites-available/subdomain.your-domain.example

添加以下配置,这样当我们从subdomain.your_domain.example来的时候,Nginx作为代理将流量重定向到端口3000

upstream subdomain.your-domain.example {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  listen [::]:80;
  server_name subdomain.your-domain.example;
  access_log /var/log/nginx/subdomain.your-domain.access.log;
  error_log /var/log/nginx/subdomain.your-domain.error.log debug;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://subdomain.your-domain.example;
    proxy_redirect off;
  }
}

我在Github上做了一个仓库,你可以克隆,vagrant-node-nginx-boilerplate

基本上,/var/www/nodeapp下的node.js应用是

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/');

nginx配置在/etc/nginx/sites-available/是

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