为什么我的 webhook 代码无法通过 HTTPS 访问

Why is my webhook code not accessible on HTTPS

我是 运行 来自机器 运行 AWS 上的 RHEL 8 的基于 apache2 的 SSL 服务器。我正在尝试在此服务器上部署一个 facebook webhook。我正在使用 curl 请求手动测试它。当我通过 HTTP 发出请求时,它的行为符合预期。但是,当通过 HTTPS 发出请求时,我收到此错误消息:

curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number 我需要让它在 HTTPS 上运行,因为 facebook 不允许仅 HTTP 连接。

任何建议都会很棒,谢谢 - 如果我问得不好,我深表歉意,这是我的第一个问题。

webhook的代码如下:


// Imports dependencies and set up http server
const
  express = require('express'),
  bodyParser = require('body-parser'),
  app = express().use(bodyParser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));

// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {

  let body = req.body;

  // Checks this is an event from a page subscription
  if (body.object === 'page') {

    // Iterates over each entry - there may be multiple if batched
    body.entry.forEach(function(entry) {

      // Gets the message. entry.messaging is an array, but
      // will only ever contain one message, so we get index 0
      let webhook_event = entry.messaging[0];
      console.log(webhook_event);
    });

    // Returns a '200 OK' response to all requests
    res.status(200).send('EVENT_RECEIVED');
  } else {
    // Returns a '404 Not Found' if event is not from a page subscription
    res.sendStatus(404);
  }

});

// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {

  // Your verify token. Should be a random string.
  let VERIFY_TOKEN = "duckgoesquack"

  // Parse the query params
  let mode = req.query['hub.mode'];
  let token = req.query['hub.verify_token'];
  let challenge = req.query['hub.challenge'];

  // Checks if a token and mode is in the query string of the request
  if (mode && token) {

    // Checks the mode and token sent is correct
    if (mode === 'subscribe' && token === VERIFY_TOKEN) {

      // Responds with the challenge token from the request
      console.log('WEBHOOK_VERIFIED');
      res.status(200).send(challenge);

    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);
    }
  }
});

我已经尝试更新我的 apache conf 文件 - 虚拟主机部分如下:

NameVirtualHost *:80

<VirtualHost *:443>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
</VirtualHost>

<VirtualHost *:80>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
</VirtualHost>

<VirtualHost *:443>
ServerName www.lloydarnoldtestapps.tk
ServerAlias *.lloydarnoldtestaps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

<VirtualHost *:1337>
ServerName lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>


<VirtualHost *:80>
ServerName www.lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
</VirtualHost>

<VirtualHost *:1337>
ServerName www.lloydarnoldtestapps.tk
DocumentRoot /var/www/lloydarnoldtestapps.tk
SSLCertificateFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/lloydarnoldtestapps.tk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

我想我明白发生了什么。
curl -vv -X POST https://www.lloydarnoldtestapps.tk 响应来自 apache Server: Apache/2.4.37

当运行curl -vv -X POST http://www.lloydarnoldtestapps.tk:1337/webhook
响应来自快递 X-Powered-By: Express

我认为当你首先开始表达并且它绑定到 port 1337 并且当你 运行 apache 它实际上无法绑定所以这就是你收到 200 的原因向 port 1337

发送请求

你不能从 apache return index.js。您将使用 apache 作为反向代理和来自 AWS (HTTPS) -> Apache (HTTP) -> Express 的代理请求。这样apache就会终止https,通过http来表达请求。

查看 Apache 的 Proxy 和 ProxyPass 以及关于 nodejs 服务器反向代理的教程。

看看这个 post

特别是这些指令

  ProxyPass / https://example.com:4433/
  ProxyPassReverse / https://example.com:4433 /

Post 如有任何问题,请返回。