我已经在服务器上设置了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服务器。因此,您需要为节点设置一个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。

其他回答

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来使用所有的核。

你也可以用Nginx设置多个域,转发到多个node.js进程。

例如实现这些:

domain1。example ->到本地运行的Node.js进程http://127.0.0.1:4000 domain2。example ->到本地运行的Node.js进程http://127.0.0.1:5000

这些端口(4000和5000)应该用来监听应用程序代码中的应用程序请求。

/etc/nginx/sites-enabled / domain1

server {
    listen 80;
    listen [::]:80;
    server_name domain1.example;
    access_log /var/log/nginx/domain1.access.log;
    location / {
        proxy_pass    http://127.0.0.1:4000/;
    }
}

在/etc/nginx/sites-enabled / domain2

server {
    listen 80;
    listen [::]:80;
    server_name domain2.example;
    access_log /var/log/nginx/domain2.access.log;
    location / {
        proxy_pass    http://127.0.0.1:5000/;
    }
}

我通过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

你也可以使用node.js生成静态文件到nginx提供的目录中。当然,站点的一些动态部分可以由node提供,而另一些则由nginx提供(静态)。

使用nginx提供的一些服务可以提高你的性能。