【发布时间】: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