我已经在服务器上设置了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。


当前回答

你也可以在一个服务器配置中为应用程序设置不同的url:

yourdomain。example/app1/* ->到本地运行的Node.js进程 http://127.0.0.1:3000 yourdomain。example/app2/* ->到Node.js进程 本地运行http://127.0.0.1:4000

在/etc/nginx/sites-enabled / yourdomain:

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.example;

    location ^~ /app1/{
        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://127.0.0.1:3000/;
    }

    location ^~ /app2/{
        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://127.0.0.1:4000/;
    }
}

重启Nginx:

sudo service nginx restart

启动应用程序。

节点app1.js

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

节点app2.js

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

其他回答

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

我通过Nginx代理独立的Node Express应用程序。

因此,新的应用程序可以很容易地安装,我也可以在同一服务器上的不同位置运行其他东西。

以下是关于我的Nginx配置示例设置的更多详细信息:

Deploy multiple Node applications on one web server in subfolders with Nginx Things get tricky with Node when you need to move your application from from localhost to the internet. There is no common approach for Node deployment. Google can find tons of articles on this topic, but I was struggling to find the proper solution for the setup I need. Basically, I have a web server and I want Node applications to be mounted to subfolders (i.e. http://myhost/demo/pet-project/) without introducing any configuration dependency to the application code. At the same time I want other stuff like blog to run on the same web server. Sounds simple huh? Apparently not. In many examples on the web Node applications either run on port 80 or proxied by Nginx to the root. Even though both approaches are valid for certain use cases, they do not meet my simple yet a little bit exotic criteria. That is why I created my own Nginx configuration and here is an extract: upstream pet_project { server localhost:3000; } server { listen 80; listen [::]:80; server_name frontend; location /demo/pet-project { alias /opt/demo/pet-project/public/; try_files $uri $uri/ @pet-project; } location @pet-project { rewrite /demo/pet-project(.*) $1 break; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $proxy_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://pet_project; proxy_redirect http://pet_project/ /demo/pet-project/; } } From this example you can notice that I mount my Pet Project Node application running on port 3000 to http://myhost/demo/pet-project. First Nginx checks if whether the requested resource is a static file available at /opt/demo/pet-project/public/ and if so it serves it as is that is highly efficient, so we do not need to have a redundant layer like Connect static middleware. Then all other requests are overwritten and proxied to Pet Project Node application, so the Node application does not need to know where it is actually mounted and thus can be moved anywhere purely by configuration. proxy_redirect is a must to handle Location header properly. This is extremely important if you use res.redirect() in your Node application. You can easily replicate this setup for multiple Node applications running on different ports and add more location handlers for other purposes.

来自:http://skovalyov.blogspot.dk/2012/07/deploy-multiple-node-applications-on.html

我们可以通过Nginx作为反向代理来轻松地设置一个Nodejs应用程序。 以下配置假设NodeJS应用程序运行在127.0.0.1:8080上,

  server{
     server_name domain.example sub.domain.example; # multiple domains

     location /{
      proxy_pass http://127.0.0.1:8080;
      proxy_set_header Host $host;
      proxy_pass_request_headers on;
     }

     location /static/{
       alias /absolute/path/to/static/files; # nginx will handle js/css
     }
   }

在上面的设置中,你的Nodejs应用程序将

get HTTP_HOST头,在那里你可以应用域特定的逻辑来服务响应。 你的应用程序必须由一个进程管理器管理,比如pm2或supervisor来处理情况/重用套接字或资源等。 设置一个错误报告服务来获取生产错误,如哨兵或滚动条

注意:你可以设置处理域特定请求路由的逻辑,为expressjs应用程序创建一个中间件

Nginx可以作为一个反向代理服务器,就像一个项目经理。当它得到一个请求时,它会分析它并将请求转发给上游(项目成员)或处理它自己。Nginx有两种处理请求的方式。

服务请求 将请求转发到另一个服务器 服务器{ server_name mydomain。示例sub.mydomain.example; 位置/ { proxy_pass http://127.0.0.1:8000; 主机$ Host; proxy_pass_request_headers; } 位置/静态/ { 别名/我/静态/文件/路径; } }

服务器请求

使用此配置,当请求URL为 Mydomain.example /static/myjs.js返回myjs.js文件 / /静态/文件/文件夹路径。当你配置Nginx服务时 静态文件,它处理请求本身。

将请求转发到另一个服务器

当请求URL是mydomain时。Nginx将转发 请求到http://127.0.0.1:8000。上运行的服务 Localhost 8000端口将接收请求并返回响应 Nginx返回客户端的响应。

当你在8000端口上运行node.js服务器时,Nginx将把请求转发给node.js。编写node.js逻辑并处理请求。你让你的nodejs服务器在Nginx服务器后面运行。

如果你想运行nodejs之外的其他服务,只需要在不同的端口上运行另一个服务,比如Django, flask, PHP,然后在Nginx中配置它。

你也可以在一个服务器配置中为应用程序设置不同的url:

yourdomain。example/app1/* ->到本地运行的Node.js进程 http://127.0.0.1:3000 yourdomain。example/app2/* ->到Node.js进程 本地运行http://127.0.0.1:4000

在/etc/nginx/sites-enabled / yourdomain:

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.example;

    location ^~ /app1/{
        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://127.0.0.1:3000/;
    }

    location ^~ /app2/{
        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://127.0.0.1:4000/;
    }
}

重启Nginx:

sudo service nginx restart

启动应用程序。

节点app1.js

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

节点app2.js

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