Flask 服务器有时会截断长 json 响应
flask server is truncating long json responses some of the times
我的路线正在获取用户令牌
GET /tokens
平均响应时间约为 180 毫秒,响应时间为 json。
使用 Flask + nginx。
一些请求,响应内容被截断到 33kb 左右,因此 JSON 格式错误。一些请求,使用相同的参数,几乎在相同的时间,响应在 216kb 左右是可以的。
我的问题是,为什么会这样,为什么会以不一致的方式发生?
这是烧瓶响应代码
class NormalResponse(Response):
def __init__(self, response):
super(NormalResponse, self).__init__(response, 200)
res = json.dumps(paginator.paginate(tokens))
return NormalResponse(res)
我发现了与 nginx 相关的问题,因为失败的响应有这个日志
2018/12/18 16:35:17 [crit] 16#16: *95010 open() "/var/tmp/nginx/uwsgi/1/42/0000000421" failed (13: Permission denied) while reading upstream, client: 172.31.72.76, server: , request: "GET /tokens?limit=501&offset=0&order=desc&owner_id=11111 HTTP/1.1", upstream: "uwsgi://unix:/run/server.socket:", host: "oauth.dev.bla_bla.com"
似乎响应溢出 proxy_buffers and tries to temporarily save it to proxy_temp_path,您的错误消息完全证实了这一点。您应该检查 nginx 用户对该文件夹的文件权限。
此问题已通过添加
解决
RUN chown -R www-data:www-data /var/tmp/nginx
到Dockerfile
我的路线正在获取用户令牌
GET /tokens
平均响应时间约为 180 毫秒,响应时间为 json。 使用 Flask + nginx。
一些请求,响应内容被截断到 33kb 左右,因此 JSON 格式错误。一些请求,使用相同的参数,几乎在相同的时间,响应在 216kb 左右是可以的。
我的问题是,为什么会这样,为什么会以不一致的方式发生?
这是烧瓶响应代码
class NormalResponse(Response):
def __init__(self, response):
super(NormalResponse, self).__init__(response, 200)
res = json.dumps(paginator.paginate(tokens))
return NormalResponse(res)
我发现了与 nginx 相关的问题,因为失败的响应有这个日志
2018/12/18 16:35:17 [crit] 16#16: *95010 open() "/var/tmp/nginx/uwsgi/1/42/0000000421" failed (13: Permission denied) while reading upstream, client: 172.31.72.76, server: , request: "GET /tokens?limit=501&offset=0&order=desc&owner_id=11111 HTTP/1.1", upstream: "uwsgi://unix:/run/server.socket:", host: "oauth.dev.bla_bla.com"
似乎响应溢出 proxy_buffers and tries to temporarily save it to proxy_temp_path,您的错误消息完全证实了这一点。您应该检查 nginx 用户对该文件夹的文件权限。
此问题已通过添加
解决RUN chown -R www-data:www-data /var/tmp/nginx
到Dockerfile