前言
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]# ls default.conf node.ruanwenwu.cn.conf test.ruanwenwu.conf www.tjzsyl.com.conf laravel.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