You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在局域网Windows主机部署Node.js应用并实现多应用路径访问

局域网部署多Node.js应用并通过路径访问的配置方案

1. 配置Node.js应用监听局域网可访问地址

每个应用需要监听0.0.0.0(允许局域网内所有设备访问),并为不同应用分配独立端口(比如firebasemiddleware用3000,anothermiddleware用3001)。以Express框架为例,修改启动代码:

// firebasemiddleware的启动文件
const express = require('express');
const app = express();
const PORT = 3000;

// 你的业务逻辑(SQL Server读写、Firebase RTDB交互等)
// ...

// 监听0.0.0.0而非localhost,确保局域网设备能连接
app.listen(PORT, '0.0.0.0', () => {
  console.log(`Firebase Middleware运行在 http://0.0.0.0:${PORT}`);
});
// anothermiddleware的启动文件
const express = require('express');
const app = express();
const PORT = 3001;

// 你的业务逻辑
// ...

app.listen(PORT, '0.0.0.0', () => {
  console.log(`Another Middleware运行在 http://0.0.0.0:${PORT}`);
});

2. 用反向代理(Nginx)实现路径转发

要通过http://主机IP/应用名称的形式访问,需要反向代理工具将路径请求转发到对应应用的端口。以Nginx为例:

安装Nginx

根据主机系统安装:

  • Ubuntu/Debian:apt install nginx
  • CentOS/RHEL:yum install nginx
  • Windows:下载官方安装包手动安装

修改Nginx配置

编辑Nginx的服务器配置文件(比如Ubuntu下的/etc/nginx/sites-available/default),添加以下规则:

server {
    listen 80;
    server_name 192.168.1.254; # 替换为主机的本地IP

    # 转发firebasemiddleware的请求到3000端口
    location /firebasemiddleware/ {
        proxy_pass http://localhost:3000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # 转发anothermiddleware的请求到3001端口
    location /anothermiddleware/ {
        proxy_pass http://localhost:3001/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

重启Nginx服务

# Ubuntu/Debian
sudo systemctl restart nginx

# CentOS/RHEL
sudo systemctl restart nginx

# Windows
进入Nginx安装目录,执行`nginx -s reload`

3. 配置主机防火墙允许局域网访问

开放80端口给局域网设备(仅允许内网访问,无需对外网开放):

Linux(UFW防火墙)

# 允许192.168.1.0/24网段访问80端口
sudo ufw allow from 192.168.1.0/24 to any port 80

Windows防火墙

  1. 打开「Windows Defender防火墙」→「高级设置」
  2. 新建「入站规则」,选择「端口」→ 勾选TCP,指定端口80
  3. 选择「允许连接」,仅勾选「专用」网络(对应局域网)
  4. 命名规则(比如「Nginx局域网访问」)并完成配置

4. 测试与验证

在局域网内的其他设备上,通过桌面应用或浏览器访问:

  • http://192.168.1.254/firebasemiddleware/
  • http://192.168.1.254/anothermiddleware/

确认能正常请求数据即可。

额外注意事项

  • 为主机设置静态本地IP:避免主机重启后IP变化导致访问失败,可在路由器后台或系统网络设置中配置静态IP。
  • 路径斜杠注意:Nginx配置中proxy_pass末尾的斜杠要和location的路径对应,否则会导致请求路径拼接错误。
  • 应用端口冲突:确保不同Node.js应用使用的端口不重复,且未被其他服务占用。

内容的提问来源于stack exchange,提问作者F0r3v3r-A-N00b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:06:17