简体   繁体   English

504-网关超时仅在 django 函数中

[英]504- Gateway timeout only in django function

I have a very mind-boggling problem and my team has struggled to solve it.我有一个非常令人难以置信的问题,我的团队一直在努力解决它。 We do have it narrowed down but not 100%.我们确实缩小了范围,但不是 100%。

Introduction介绍

We are trying to implement LTI in a Django app with the Vue frontend.我们正在尝试使用 Vue 前端在 Django 应用程序中实现 LTI。 To fetch the token from the URL the backend makes a POST request to the URL with data and should receive a token or error if it's expired or invalid.为了从 URL 中获取令牌,后端向 URL 发出带有数据的POST请求,如果令牌过期或无效,则应该收到令牌或错误。

Design设计


Browser ---- POST REQUEST --> view function on Server (Django) --- POST REQUEST --> Auth URL浏览器 ---- POST REQUEST --> Server (Django) --- POST REQUEST --> Auth URL

Problem问题


The post request that Django view makes times out with 504 Gateway Timeout . Django 查看的 post 请求以504 Gateway Timeout This could be normal if the server takes a lot of time.如果服务器花费大量时间,这可能是正常的。 However, increasing the time did not help and checked Auth URL with POSTMAN it worked fine and was not down.但是,增加时间并没有帮助,并使用 POSTMAN 检查了 Auth URL,它工作正常并且没有关闭。

What I have tried我试过的


We decided to debug or diagnose this issue that why a code block works in a function when it is called through a shell but not when it is called by a POST request does not.我们决定调试或诊断这个问题,即为什么代码块在通过 shell 调用时在函数中起作用,但在被POST请求调用时却不起作用。

  1. Eliminated the front end and used POSTMAN to send a POST request to my Django server -- timeout on the Auth URL消除了前端并使用POSTMAN向我的 Django 服务器发送POST请求——Auth URL 超时
  2. Called the same function using Django shell -- worked使用Django shell调用相同的函数——工作
  3. Copied the code into a separate python file outside Django -- worked将代码复制到 Django 之外的单独 python 文件中 - 工作
  4. Used same virtual env for all above以上所有使用相同的虚拟环境

What it appears to be它看起来是什么


When a POST function is called and another post request is made inside it then it times out.当调用 POST 函数并在其中发出另一个 post 请求时,它会超时。 Please note: If I make a POST request in the same situation with invalid data (say missing grant_type ) then it does not time out.请注意:如果我在相同情况下使用无效数据(比如缺少grant_type )发出POST请求,那么它不会超时。


Code Block代码块


auth_request = {
    "grant_type": "client_credentials",
    "client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
    "client_assertion": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlJVbzNJWWlSR0ZDYUhNNEg0S2lid095enAtRU9KWlAweXkwd0g3bk5VOEEifQ.eyJpc3MiOiI2NjkxNmZmYy03YjE5LTQ0MWEtYjE5Zi0yOGQxMzVmYjZjOWYiLCJzdWIiOiI2NjkxNmZmYy03YjE5LTQ0MWEtYjE5Zi0yOGQxMzVmYjZjOWYiLCJhdWQiOiJodHRwczovL2RldmVsb3Blci5ibGFja2JvYXJkLmNvbS9hcGkvdjEvZ2F0ZXdheS9vYXV0aDIvand0dG9rZW4iLCJpYXQiOjE2NTMyODcyNzMsImV4cCI6MTY1MzI4NzMzOCwianRpIjoibHRpLXNlcnZpY2UtdG9rZW4tMDQyZTZhNjctNDA2My00YmQ1LWI2NmQtNTM4YjU2ZTllM2Q1In0.8Jaou965cPTCFv-7yP9iIlH8mMgQjAi0AR2li0KwCcRuHsRZ_1OpbE83bZ06RMXhbjA4crRqTI4zMi8aNfq16Mkg4lXoPj8JiJW7q8b_ZQ1rLZvIojmabehYjpyscHRitFPLibfTYF2mCjUyHqwPgnFRLNrHIVuSvM0BiK56PuYK6SiiSjxu2U3bmJqOHNW2mqx2YYfkaXx2u7ru6CKTiL3KBGzFPYjCUwwWNBdbz4R0g0aHK_l-hhA3oi_pCDZOyqdnyCmGAj5SpZbuZOqrZbQBrqPoEFtXdNDPpHGGwW7IUbbmCtsmE2NqQiYt6snmK-1pbxsLxE0mXrpDqASh4A",
    "scope": "https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly",
}
response = requests.post(
    "https://developer.blackboard.com/api/v1/gateway/oauth2/jwttoken",
    data=auth_request,
)
response = response.json()
print(response)

After days of trying to figure it out, we deployed the project with Production settings and it worked.经过几天的尝试,我们使用Production设置部署了该项目,并且成功了。 Upon investigation on why it was not working on staging we found the following:在调查了它为什么不能进行分期工作后,我们发现了以下内容:

  1. Front end sends a POST request to the back end前端向后端发送 POST 请求
  2. Backend then encoded the data using the private key and sent it to a 3rd party server然后后端使用私钥对数据进行编码并将其发送到第 3 方服务器
  3. Because 3rd party server needed to validate our jwks message it sent another request to a URL on our staging server.因为第 3 方服务器需要验证我们的jwks消息,所以它向我们登台服务器上的 URL 发送了另一个请求。
  4. This 3rd request was not addressed as the server only had one thread.由于服务器只有一个线程,因此未解决第三个请求。 Adding a threads parameter to gunicorn did the trickgunicorn添加线程参数就可以了

gunicorn ripple.wsgi --reload --log-level debug --threads 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM