nginx使用反向代理支持node.js服務


前言

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM