【问题标题】:How to deploy a Telegram Bot with Nginx reverse proxy如何使用 Nginx 反向代理部署 Telegram Bot
【发布时间】:2018-06-20 19:00:20
【问题描述】:

我使用python-telegram-bot 库开发了一个 Telegram 机器人,现在我想将它部署在我的服务器中,所以我设置了一个 webhook(遵循Official Wiki)但是当我尝试与我的机器人通信时我没有得到任何答复。

这是机器人的来源:

def main():
   PRIVTOKEN = "1134xxxx"
   updater = Updater(PRIVTOKEN)

   dp = updater.dispatcher

   dp.add_handler(CommandHandler("start", start))
   # ...

   updater.start_webhook(listen='127.0.0.1',
                         port=8843,
                         url_path=PRIVTOKEN)
   updater.bot.set_webhook(webhook_url='https://example.com/' + PRIVTOKEN,
                           certificate=open("cert.pem", "rb"))
   print("bot started")
   updater.idle()

nginx 配置文件:

server {
  listen 443 ssl;
  server_name example.com;


  location /1134xxxx {
     proxy_pass http://127.0.0.1:8443;
  }
}

netstat 状态:

sudo netstat -an | grep 8843
tcp        0      127.0.0.1:8843            0.0.0.0:*               LISTEN 

机器人(我已启用错误日志)或 nginx(access/error.log)没有记录其他错误

我还在防火墙中为 8843 端口添加了自定义规则。

【问题讨论】:

  • 请重新检查您的描述 - 您的 python 应用程序侦听 8843,但 nginx 代理到端口 8443(netstat 很烂,我建议使用 lsof -i :8443)
  • 修复这个拼写错误也不起作用
  • 我没说会有帮助,我只是想清楚地了解情况。请更新初始描述。然后我建议逐跳检查:确保您的数据包可以到达 Nginx,然后确保来自 Nginx 的数据包可以到达您的应用程序。例如,您可以使用 tcpdump 或使用某种日志记录(包括 nginx 日志)
  • 尝试向您的服务器添加一个letsencrypt免费https证书

标签: python nginx telegram-bot python-telegram-bot


【解决方案1】:

Telegram 仅支持 https 请求。注意这一点。

我的 nginx 配置:

server {
        listen 80;
        listen 443 ssl;
        server_name example.com www.example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        if ($scheme = http) {
            return 301 https://$server_name$request_uri;
        }
    location / {
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://localhost:5005/;
    }
}

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2014-08-26
    • 2020-09-24
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多