前言
Node.js自身能作为web服务器用,但是如果要在一台机器上开启多个Node.js应用该如何做呢?有一种答案就是使用nginx做反向代理。反向代理在这里的作用就是,当代理服务器接收到请求,将请求转发到目的服务器,然后获取数据后返回。
步骤
一、正常使用node.js开启web服务
var http = require(‘http‘);http.createServer(function (request, response) { ???response.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); ???response.end(‘hello world\n‘);}).listen(1337);console.log(‘Server running at http://127.0.0.1:1337/‘);
二、为域名配置nginx
[root@iZ25lzba47vZ vhost]# lsdefault.conf ??????????????node.ruanwenwu.cn.conf ?test.ruanwenwu.conf ???www.tjzsyl.com.conflaravel.ruanwenwu.cn.conf ?wss.ruanwenwu.cn.conf ?www.tjzsyl.com.conf.bak[root@iZ25lzba47vZ vhost]# pwd
node.ruanwenwu.cn.conf:
server{ ???listen 80; ???server_name node.ruanwenwu.cn; ???location / { ???????proxy_pass http://127.0.0.1:1337; ???}}
步骤很简单,这些就可以了。
最后看一下我们的效果:http://node.ruanwenwu.cn
nginx使用反向代理支持node.js服务
原文地址:http://www.cnblogs.com/doubilaile/p/8017463.html