首页 > 解决方案 > Apache2 上的代理错误 502,用于反向代理到 Digital Ocean 上的 NodeJs 子域应用程序

问题描述

大家好

我试图在 Digital Ocean 上部署我的 nodejs api,在子域https://api.host.com下。它有一些路径波纹管,都从 /v0.1 开始。

例如https://api.host.com/v0.1/users

在这个 droplet 上,nodejs 应用程序正在http://localhost:3000上运行

在同一个水滴中,我有其他应用程序,一个托管在 Apache 上并在https://host.com上响应的网站,它运行良好。

我使用 Apache 进行反向代理从https://api.host.com/http://localhost:3000进行所有调用。

实际上它仅适用于根路径,我的意思是尝试访问https://api.host.com/响应正确但是当我尝试访问其他路径时出现错误

Proxy Error

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request

Reason: DNS lookup failure for: 127.0.0.1:3000v0.1

Apache/2.4.29 (Ubuntu) Server at api.host.com Port 443

HTTP 状态码:502 代理错误

我的虚拟主机看起来像......

<VirtualHost *:80> 
        ...
        ...
        ServerName api.host.com 
        ProxyPass /.well-unknown/ !
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
       <Proxy *>
          Require all granted
      </Proxy>

      ProxyPass / http://127.0.0.1:3000/ 
      ProxyPassReverse / http://127.0.0.1:3000/ 
      ...
      ...
 </VirtualHost>

请帮忙!

标签: node.jsapachereverse-proxy

解决方案


我正在使用 Apache 服务器将请求从子域通过代理转发到侦听端口 8080 的 nodeJS 应用程序做同样的事情。

我相信您只需要将根代理传递给 localhost:3000 并且 nodeJS 应用程序应该处理路由。

例如,如果您使用的是 express,则类似于.....

app.get('/v.01/users/', function (req, res) {
   res.sendFile('/v.01/users/index.html'); });

推荐阅读