[英]HTTP2 setup for NodeJS Express on Apache throws Error
我無法為通過 Apache 服務器提供的 NodeJS v12.19.0/Express App 設置 HTTP2。
我啟用了 mod http2和proxy_http2並安裝了npm http2 。
bin/www 如下所示:
var app = require('../app');
var debug = require('debug')('production:server');
var http = require('http2');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
Apache 站點配置:
<VirtualHost *:443>
Protocols h2 http/1.1
ServerName domain.com
ProxyRequests On
#ProxyPass / http://localhost:3000/
#ProxyPassReverse / http://localhost:3000/
ProxyPass / h2://localhost:3000/
ProxyPassReverse / http://localhost:3000/
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
沒有線條
ProxyPass / h2://localhost:3000/
ProxyPassReverse / http://localhost:3000/
我得到:
代理錯誤
代理服務器收到來自上游服務器的無效響應。 代理服務器無法處理請求
原因:從遠程服務器讀取錯誤
Apache/2.4.41 (Ubuntu) 服務器在 domain.com 端口 443
如果我包含這些行,我會得到:
內部服務器錯誤
服務器遇到內部錯誤或配置錯誤,無法完成您的請求。
請通過 [no address given] 聯系服務器管理員,告知他們此錯誤發生的時間,以及您在此錯誤之前執行的操作。
服務器錯誤日志中可能提供有關此錯誤的更多信息。 Apache/2.4.41 (Ubuntu) 服務器在 domain.com 端口 443
幫助表示贊賞..
您正在嘗試通過 HTTPS ( h2
) 上的 HTTP/2 從 Apache 連接到節點,但從您的 Node 代碼看來,您使用的是 TLS 證書,所以我猜它只是一個 HTTP/2 over HTTP 服務器( h2c
)。
所以替換這些行:
ProxyPass / h2://localhost:3000/
ProxyPassReverse / http://localhost:3000/
有了這個:
ProxyPass / h2c://localhost:3000/
ProxyPassReverse / http://localhost:3000/
它應該通過 HTTP/2 連接。
但是我會質疑這個的價值。 Mod_proxy_http2仍然被標記為實驗性的,后端 HTTP/2 連接的價值是非常有問題的。 如果您希望使用 HTTP/2 在 Node 中進行實驗,那么您不應該直接連接而不是通過 Apache 連接嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.