node.js 与 nginx 配合使用

现在小介绍下怎么用 nginx 和 node.js 配合使用。

var http = require('http');  
http.createServer(function (request, response) {  
    response.writeHead(200, {'Content-Type': 'text/plain'});  
    response.end('hello world\n');  
}).listen(8000);  
console.log('Server running at http://127.0.0.1:8000/');

然后 node helloworld.js 运行,这样跑在本地的机子的 nodejs 的程序就运行起来了,占用的是 8000 端口,可自己修改。

接着,我们在 nginx 的 vhost.conf 里面写一个做一个代理

server {  
    listen  80;  
    server_name nodejs.fbzl.org;  
    location / {
        proxy_pass http://127.0.0.1:8000;  
    }  
}

将网站域名设置好,然后端口设置为80,最后proxy_pass设置为http://127.0.0.1:8000,将所有从taqing.me:80的请求传递到nodejs程序去。

重启 nginx

访问域名,就可以了看到 hello world 了。

虽然 node.js 本身就可以做服务器是没错啦,比如 welcome.js 里面设置为 80 端口就可以了。

但是使用 nginx 有神奇加成

除非注明, 风波竹林 文章均为原创,转载请以链接形式标明本文地址
本文地址: https://blog.fbzl.org/node-js与-nginx配合使用/

发表评论