【问题标题】:502: Proxy Error from Express app on Ubuntu 18.04 & Apache2502:来自 Ubuntu 18.04 和 Apache2 上 Express 应用程序的代理错误
【发布时间】:2021-09-24 08:44:50
【问题描述】:

我正在尝试在 Ubuntu 18.04 上创建一个简单的 Express & Node 应用程序,它应该像一个 API。问题是,每当尝试访问 url 时,我只会收到 502 错误。下面是用于配置 Apache 和 express 应用程序本身的代码,以及日志中的条目。

App.js

const express = require('express')
const http = require('http')
const cors = require('cors')

const app = express()
app.use(cors())
app.enable('trust proxy')

const port = 3500

app.get('/', function (req, res) {
        console.log('asdfasdf')
})

http.createServer(app).listen(port, () => {
        console.log('Running http server on port %s', port);
});

Apache 配置(非 SSL):

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/node2.domain.io
        ServerName node2.domain.io
        ServerAlias www.node2.domain.io

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full

   <Proxy *>
      Require all granted
   </Proxy>

   <Location />
      ProxyPass https://127.0.0.1:3500/
      ProxyPassReverse https://127.0.0.1:3500/
   </Location>

        <Directory /var/www/node2.domain.io>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =node2.domain.io [OR]
        RewriteCond %{SERVER_NAME} =www.node2.domain.io
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Apache 配置 (SSL):

</VirtualHost>
</IfModule>

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/node2.domain.io
        ServerName node2.domain.io
        ServerAlias www.node2.domain.io

 SSLProxyEngine On
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full

   <Proxy *>
      Require all granted
   </Proxy>

   <Location />
      ProxyPass https://127.0.0.1:3500/
      ProxyPassReverse https://127.0.0.1:3500/
   </Location>

        <Directory /var/www/node2.domain.io>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

SSLCertificateFile /etc/letsencrypt/live/node2.domain.io/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/node2.domain.io/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

日志条目:

AH01102: error reading status line from remote server 127.0.0.1:3500, referer: https://node2.domain.io/

^ 这个 x 200

附带说明,当我启动前端 React 应用程序时,它可以正常工作。

【问题讨论】:

  • 您尝试通过 https 访问您的节点应用程序,但我看到您的节点应用程序中只运行了一个 http 服务。
  • 将 http.createServer 替换为 app.listen,现在可以使用了。

标签: node.js apache express ubuntu


【解决方案1】:

在您的代码中,您永远不会返回对请求的响应:

app.get('/', function (req, res) {
        console.log('asdfasdf')
})

当我们打电话给/ 时,你告诉 express 做console.log('asdfasdf') 而不是别的。

返回这样的东西,你会得到你的回应:

app.get('/', function (req, res) {
        console.log('asdfasdf')
        res.send('ok') // <-- add this line
})

另外,如果你想创建一个API,你可能想返回JSON响应,所以你可以使用Express的res.json()方法:

app.get('/', function (req, res) {
        console.log('asdfasdf')
        res.json({
            name: 'toto',
            foo: 'bar'
        })
})

【讨论】:

  • 目前,我没有尝试返回任何东西 - 只是为了看到它在终端中给我一条消息。我已经添加了那条线,仍然没有结果。我也尝试在 https 上提供应用程序。
  • 是的,但这是问题所在,您返回任何内容,然后,如果您的 express 服务器返回任何内容,Apache 会给您一个 502 错误,因为 apache 没有收到您的 Express 服务器的响应。您是否重新加载您的节点应用程序以使更改生效?
  • 更改没有奏效,但我设法找到了解决方案。谢谢您的帮助!我会记住你说的话。祝您有美好的一天!
【解决方案2】:

我已经用 app.listen(port) 替换了 http.createServer(/* .. */) 并且它现在可以工作了。

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 2020-03-18
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多