502:来自 Ubuntu 18.04 和 Apache2 上的 Express 应用程序的代理错误
502: Proxy Error from Express app on Ubuntu 18.04 & Apache2
我正在尝试在 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 应用程序时,它可以正常工作。
在您的代码中,您永远不会 return 对请求的响应:
app.get('/', function (req, res) {
console.log('asdfasdf')
})
当我们调用 /
时,你告诉 express 做 console.log('asdfasdf')
而不是别的。
return 这样的事情,你会得到你的回应:
app.get('/', function (req, res) {
console.log('asdfasdf')
res.send('ok') // <-- add this line
})
此外,如果你想创建一个API,你可能想要return JSON响应,所以你可以使用Express的res.json()
方法:
app.get('/', function (req, res) {
console.log('asdfasdf')
res.json({
name: 'toto',
foo: 'bar'
})
})
我已经将 http.createServer(/* .. */) 替换为 app.listen(port),现在可以使用了。
我正在尝试在 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 应用程序时,它可以正常工作。
在您的代码中,您永远不会 return 对请求的响应:
app.get('/', function (req, res) {
console.log('asdfasdf')
})
当我们调用 /
时,你告诉 express 做 console.log('asdfasdf')
而不是别的。
return 这样的事情,你会得到你的回应:
app.get('/', function (req, res) {
console.log('asdfasdf')
res.send('ok') // <-- add this line
})
此外,如果你想创建一个API,你可能想要return JSON响应,所以你可以使用Express的res.json()
方法:
app.get('/', function (req, res) {
console.log('asdfasdf')
res.json({
name: 'toto',
foo: 'bar'
})
})
我已经将 http.createServer(/* .. */) 替换为 app.listen(port),现在可以使用了。