【问题标题】:502 Bad Gateway on WSGI app using Nginx + Cherrypy使用 Nginx + Cherrypy 的 WSGI 应用程序上的 502 Bad Gateway
【发布时间】:2013-06-16 14:35:07
【问题描述】:

我有一个 Python 应用程序通过 CherryPy 的 WSGI 服务器接收来自 Nginx 的请求。它托管在 Windows Server 2003 机器上,只有少数用户访问它。

有时,对特定资源的请求几乎会立即失败,并在 Firefox 上出现 502 Bad Gateway。我尝试在其他浏览器上重现该问题,但没有成功。

应用程序是这样启动的:

paths = { "/resource": resource_func }
dispatcher = wsgiserver.WSGIPathInfoDispatcher(paths)
server = wsgiserver.CherryPyWSGIServer(("127.0.0.1", 9191), dispatcher)
server.start()

nginx 服务器正在通过proxy_pass 向 CherryPy 转发请求:

location / {
    proxy_pass         http://127.0.0.1:9191/;
    proxy_redirect     off;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

请求通过 jQuery.ajax 方法发送:

$.ajax(requestURI).done(myRequestCallback);

如果我运行 Firebug,然后在出现 502 时打开 nginx 错误日志,我会看到以下错误:

(time): *92 WSASend() failed (10054: An existing connection was forcibly closed by the remote host) while sending request to upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET /resource?params=VALUES&_=1371674388573 HTTP/1.1", upstream: "http://127.0.0.1:9191/resource?params=VALUES&_=1371674388573", host: "xxx.xxx.xxx.xxx:9080", referrer: "http://172.27.129.112:9080/myAppPage.html"

...其中xxx.xxx.xxx.xxx 是我正在测试应用程序的IP,而nginx 正在监听9080。

失败的请求不会显示在我们应用的日志文件中,并且相同的资源和参数是有效的(请求不会在每次尝试时都失败)。

我什至在我的 nginx.conf 文件中添加了以下几行(然后重新启动了两台服务器)以尝试解决问题,但似乎没有任何区别:

proxy_buffers 8 16k;
proxy_buffer_size 32k;

版本是:

  • 火狐21
  • Python 2.7.5
  • nginx 1.2.0
  • CherryPy 3.2.2

什么可能导致错误?

【问题讨论】:

    标签: python firefox nginx wsgi cherrypy


    【解决方案1】:

    当我看到这个(不是使用 CherryPy,而是使用 Gunicorn/others)时,这是因为服务器正在重新启动并且 Nginx 无法连接到它。服务器上是否发生了其他可能导致 CherryPy 重启的事情?

    【讨论】:

    • 我从脚本启动 WSGI 服务器,除非 CherryPy 在内部重新启动它的任何组件,否则在我看到错误时它并没有停止。我发现有趣的是我只能在 Firefox 上重现它......我也看过 this question 但它与日志上的错误不同。
    猜你喜欢
    • 2020-12-14
    • 2014-08-04
    • 2020-04-22
    • 2017-07-31
    • 1970-01-01
    • 2019-08-10
    • 2019-10-17
    • 2016-04-09
    • 2018-08-03
    相关资源
    最近更新 更多