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

mac2022-06-30  20

前言

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

转载于:https://www.cnblogs.com/doubilaile/p/8017463.html

最新回复(0)