Django集成Playwright部署后触发Nginx 502 Bad Gateway错误的排查求助
Django集成Playwright部署后触发Nginx 502 Bad Gateway错误的排查求助
我碰到了一个非常棘手的问题:我的Django应用里,所有涉及Playwright的功能模块都无法正常运行。本地开发环境下一切正常(包括Playwright的所有操作),但部署到服务器后,访问不需要Playwright的首页完全没问题,只要触发用到Playwright的代码,就直接返回502 Bad Gateway,Nginx版本是1.18.0(基于Ubuntu)。
我已经尝试了一些排查手段,但都没解决问题:
- 关闭了服务器防火墙,没有任何改善
- 逐行添加异常抛出做调试,结果很不稳定:有时候代码能执行几行,有时候刚进入Playwright逻辑就挂了,而且只有调用Playwright的代码才会触发这个502错误
- 每次执行
sudo reboot重启服务器后,功能能正常运行几分钟,但过一会儿又会变回502错误
我的技术栈版本信息:
- Django 5.1.4
- Playwright 1.49.1(当前最新版)
- 服务器系统:Ubuntu 22.04.5 LTS
以下是我用到的Playwright核心代码片段:
try: await page.goto('https://pantry.plentifulapp.com/login') await expect(page.get_by_test_id("LoginForm-44")).to_be_visible(timeout=3000) except: content = "Plentiful is down." return content # Find email input and type it. await page.get_by_test_id("LoginForm-47").click() await page.keyboard.type(username) # Find password input, type it, and then press enter. await page.get_by_test_id("LoginForm-53").click() await page.keyboard.type(password) await page.keyboard.press("Enter")
备注:内容来源于stack exchange,提问作者stelity
相关产品推荐
相关产品推荐

