【问题标题】:How to redirect all traffic from nginx to nodeJS with proxy-pass如何使用 proxy-pass 将所有流量从 nginx 重定向到 nodeJS
【发布时间】:2020-01-07 22:58:00
【问题描述】:

当我设置简单的 nginx 位置以捕获所有到 root 的流量并代理传递到端口 3000 上的 nodeJS 时,当我请求 http://example.com/something

当我进入 mydomain.net 时,我从 nodeJS 得到结果,但是当我尝试 example.com/something 时,我得到 502 Bad Gateway。

server {
        listen xxx.xxx.xxx.xxx:80;
        server_name example.com  www.example.com;

        location / {
                proxy_pass http://xxx.xxx.xxx.xxx:3000/;
        }

}

我希望 nodeJS 能够处理 url 的其余部分(在 / 之后),因为当 nodeJS 单独在端口 80 上时,它可以在没有 nginx 的情况下工作。

【问题讨论】:

    标签: node.js nginx nginx-reverse-proxy


    【解决方案1】:

    在文件中使用以下代码,并确保将 example.com 更改为您的域(或 IP),并将 1337 更改为您的 Node.js 应用程序端口:

    server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:3000";
    }
    }
    

    sudo nginx -t 
    sudo service nginx restart
    

    【讨论】:

    • 感谢 anwser,但在非根 url 上仍然出现错误网关,例如:www.example.com/test
    【解决方案2】:

    我发现 nginx 代码没问题,我的 SSL 配置似乎在这里出现问题,在检查了 nginx 的错误日志后,再次启动并成功获取 NodeJS 的所有位置。谢谢大家。

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 2016-03-18
      • 2012-04-19
      • 2012-01-25
      • 2019-08-26
      相关资源
      最近更新 更多