Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

node.js - Node.js + Nginx-现在怎么办?(Node.js + Nginx - What now?)

I've set up Node.js and Nginx on my server.

(我已经在服务器上设置了Node.js和Nginx。)

Now I want to use it, but, before I start there are 2 questions:

(现在,我想使用它,但是在开始之前,有两个问题:)

  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:

    (Node.js服务器有两个概念,其中一个更好:)

    a.

    (一种。)

    Create a separate HTTP server for each website that needs it.

    (为每个需要它的网站创建一个单独的HTTP服务器。)

    Then load all JavaScript code at the start of the program, so the code is interpreted once.

    (然后在程序开始时加载所有JavaScript代码,因此代码将被解释一次。)

    b.

    (b。)

    Create one single Node.js server which handles all Node.js requests.

    (创建一个处理所有Node.js请求的单个Node.js服务器。)

    This reads the requested files and evals their contents.

    (这将读取请求的文件并评估其内容。)

    So the files are interpreted on each request, but the server logic is much simpler.

    (因此,每个请求都将解释文件,但是服务器逻辑要简单得多。)

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

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

  ask by Van Coding translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nginx works as a front end server, which in this case proxies the requests to a node.js server.

(Nginx用作前端服务器,在这种情况下,它将代理请求发送到node.js服务器。)

Therefore you need to setup an nginx config file for node.

(因此,您需要为节点设置一个nginx配置文件。)

This is what I have done in my Ubuntu box:

(这是我在Ubuntu框中完成的操作:)

Create the file yourdomain.com at /etc/nginx/sites-available/ :

(在/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;
    }
 }

If you want nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the location / section:

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

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

Create your node server app at /var/www/yourdomain/app.js and run it at localhost:3000

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

Test for syntax mistakes:

(测试语法错误:)

nginx -t

Restart nginx:

(重新启动nginx:)

sudo /etc/init.d/nginx restart

Lastly start the node server:

(最后启动节点服务器:)

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

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

(现在,您应该在yourdomain.com上看到“ Hello World”)

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 .

(有一个关于upstart和monit的很棒的教程 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...