Django + gunicorn + nginx电报webhook ssl麻烦

我试图设置一个django作为webhooks电报机器人。 我使用这个命令创build一个ssl证书:

openssl genrsa -out webhook_pkey.pem 2048 openssl req -new -x509 -days 3650 -key webhook_pkey.pem -out webhook_cert.pem # In Common Name i type a server IP address 

安装程序在端口800上运行gunicorn,将nginx中的proxy_pass设置为localhost:800。 网站正常工作,我可以打开浏览器,看到我的主页。 Gunicorn从这个命令开始:

 gunicorn wsgi -b 127.0.0.1:800 # wsgi is django generated wsgi.py file 

我的nginx.conf文件:

 upstream django { server 127.0.0.1:800; } server { listen 80; listen 443 ssl; server_name 111.111.111.111 # these ip addres of my server ssl_certificate /path/to/webhook.cert; ssl_certificate_key /path/to/webhook.pkey; charset utf-8; client_max_body_size 10M; location /media { alias /path/to/media; } location /static { alias /path/to/static; } location / { proxy_pass http://127.0.0.1:800; proxy_set_header Host $server_name; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Protocol $scheme; } } 

我使用pyTelegramBotApi。

 from telebot import TeleBot bot = TeleBot(token) cert = open('webhook.cert', 'r') bot.remove_webhook() bot.set_webhook("https://111.111.111.111:80/user_bots/, certificate=cert) # I'm trying a differents ports cert.close() 

user_bots – 这是在Django中查看一些path,从请求中logging一些数据在我运行这段代码后,我在nginx access.log看到了这个:

 149.154.167.200 - - [26/Oct/2017:15:36:13 +0000] "\x16\x03\x01\x00\xC3\x01\x00\x00\xBF\x03\x03\x19\xB3\x937v\x14\xF0\xDCj\xC1\x93\xB2?\xF9tOK\x10\x9FA\x87|\xA9!\x81e\xCFC\xDD\x92\x94\x97\x00\x008\xC0,\xC00\x00\x9F\xCC\xA9\xCC\xA8\xCC\xAA\xC0+\xC0/\x00\x9E\xC0$\xC0(\x00k\xC0#\xC0'\x00g\xC0" 400 182 "-" "-" # And then many some logs, it stopped after i run bot.remove_webhook() 

谷歌说这是错误的SSL证书。

django urls.py

 urlpatterns += url(r'user_bots/(?P<token>[0-9]+:\w*)/.*$', views.bot_test, name="bot_test") 

bot_test view

 def bot_test(request, token): logging.info(f"Request from bot with token {token}") return HttpResponse("OK") 

我需要我的Django应用程序select什么机器人需要做的引用它的令牌。

我stucked :(我想整天解决这个问题,也许有人可以帮助我?

解决方法很简单:)替换

 cert = open(PATH_TO_CERT, 'r') bot.set_webhook(url=f'https://111.111.111.111:80/user_bots/{token}/', certificate=cert) 

 bot.set_webhook(url=f'https://111.111.111.111:443/user_bots/{token}/', certificate=open(PATH_TO_CERT, 'r') 

而且一切正常!