【问题标题】:Remote access to node.js远程访问 node.js
【发布时间】:2021-12-12 04:17:14
【问题描述】:

我想使用 webbrowser 远程运行我的 python 脚本。在我的 LAN 服务器上,我已经安装了 node.js。如果我在我的服务器上通过 webbrowsrer 启动这些脚本,一切正常。如果我尝试在远程机器上启动脚本,我看不到任何响应,但在服务器脚本启动(请求没有响应?)。代理 nginx 在服务器上也没有任何帮助。 总而言之,应用程序只能从本地主机访问,不能从远程机器访问。

此时,我的脚本应用程序只能从 localhost 访问。我无法从远程机器访问它。

Node.js index.js 文件是:

    const express =  require('express');
    const { spawn } = require('child_process');
    const app = express();
    const port = 8811;

    app.get('/script', (req, res) => {
        let data1;
        const pythonOne = spawn('python3', ['script.py']);
        pythonOne.stdout.on('data', function(data){
            data1 = data.toString();
        })

        pythonOne.on('close', (code) => {
            console.log("code", code)
            res.send(data1);
        })  
    })

    app.listen(port, () => console.log('node python script app listening        on port ' + port));

我的服务器是 192.168.1.161 在服务器上使用http://192.168.1.161:8811/script,script.py 启动正常。 在远程机器上使用http://192.168.1.161:8811/script script.py 启动,但仅在服务器上。

另外安装了带有 script.conf 文件的 nginx:

    upstream backend {
    server localhost:8811;
    keepalive 32;
    }
    server {
    listen 8500;
    server_name script.example.com;
    location / {
        client_max_body_size 50M;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
        proxy_pass http://backend;
    }       
    }

现在, 在服务器 script.py 上使用 http://192.168.1.161:8500/script 启动正常。 在远程机器上使用http://192.168.1.161:8500/script script.py 启动,但仅在服务器上。

任何答案将不胜感激。

【问题讨论】:

    标签: python node.js nginx


    【解决方案1】:

    将节点全部绑定到 0.0.0.0

    app.listen(port,'0.0.0.0' () => console.log('node python script app listening        on port ' + port));
    

    【讨论】:

      猜你喜欢
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2011-09-04
      • 2012-11-23
      • 2018-07-23
      • 2011-08-13
      • 2019-04-04
      相关资源
      最近更新 更多