您的位置:首页 > 运维架构 > Nginx

nginx and node.js配合使用 helloworld

2011-08-24 09:39 661 查看
nginx是最好的反向代理服务器。

node.js是。。。 好吧 ,不介绍了,猛击这里

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

先写个helloworld.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 ' target='_blank'>http://127.0.0.1:8000/');[/code] 
然后用node helloworld.js指令开启,这样跑在本地的机子的nodejs的程序就算开起来了,占用的是8000端口,可自己修改。

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

server {
listen	80;
server_name taqing.me www.taqing.me;
location / {
proxy_pass http://127.0.0.1:8000; }
}


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

重启nginx

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

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

但是一个机子跑多个网站,其他网站又是用别的服务器,在80端口已经被占用的情况下,是可以用代理到别的端口来处理的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: