【问题标题】:Play Framework POST endpoint returning Time Out - 504播放框架 POST 端点返回超时 - 504
【发布时间】:2017-04-19 19:08:54
【问题描述】:

我正在运行基于 Play Framework 2.5.10 构建的 REST API 产品。它通过 NGINX 反向路由运行,我能够到达所有 GET 端点,但我在所有 POST 端点上都超时,所有这些都消耗 JSON。

请注意,在开发环境中它工作正常,我能够访问所有这些端点,但在生产环境中,无论是通过 IP 连接还是通过反向路由 DNS 连接,我都会在 POST 上超时。

高度赞赏任何解决此问题的指针。

server {
listen 80;
server_name subdomain.domain.com;

location / {
    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass http://xxx.xxx.xxx.xxx:3000;
  }   
}

路由尝试访问

POST    /auth                       controllers.Application.authenticate()

我需要在 nginx 上定义所有的路由吗?

添加授权码

@BodyParser.Of(BodyParser.Json.class)
public Result authenticate(){
    JsonNode json = request().body().asJson();
    EncryptUtil secure = null;
    secure=EncryptUtil.getSecurityUtility();
    String command = "login";
    String logincommand = json.findPath("command").asText();
    if (logincommand.equals(command)){
        String email = json.findPath("email").textValue();
        String password = json.findPath("password").textValue();
        Logger.info("Passwords::"+password+"\t"+secure.getEncryptedUserPassword(password.trim()));
        UserAccount user=UserAccount.findByEmail(email);
        if(user!=null){
            if(!(secure.getDecryptedUserPassword(user.password).equals(password))){
                return status(400,"Invalid credentials");
            }else {
                if (user.accountstatus == Boolean.FALSE){
                    result.put("error","Account Deactivated Contact Admin");
                    return status(400,"Account Deactivated Contact Admin");

                } else {
                    String authToken = user.createToken();
                    ObjectNode authTokenJson = Json.newObject();
                    authTokenJson.put(AUTH_TOKEN, authToken);
                    response().setCookie(Http.Cookie.builder(AUTH_TOKEN, authToken).withSecure(ctx().request().secure()).build());
                    JsonNode userJson = Json.toJson(user);
                    return status(200,userJson);
                }

            }

        }
        else{
            result.put("Error", "Invalid User");
            Logger.info(result.toString());
            return status(400,"Invalid Credentials");
        }
    } else{
        return globalFunctions.returnBadRequest(command);
    }

}

【问题讨论】:

  • 分享 POST 操作的方法(在你的控制器中)以及 nginx conf 的相关部分。
  • @marcospereira 我已经发布了您要求的信息。
  • 您能否确认 POST 请求确实到达了您的应用程序?您能否也发布authenticate 方法的代码?
  • @marcospereira 它到达但确实需要很长时间
  • “它达到但确实需要很长时间”所以,是不是响应(并且您在方法内部有与数据库的连接)慢于 300 秒?尝试测量 GET 响应的时间间隔,以及在 POST 方法中访问数据库的时间间隔。

标签: nginx playframework connection-timeout http-status-code-504


【解决方案1】:

如果你的 POST 请求体很大并且连接很慢和/或 nginx 存储临时文件的介质很慢,这可能是由于 nginx 默认进行的请求缓冲造成的。

在它的日志文件中,nginx 还应该警告缓存到磁盘的大型主体。

如果这是您的问题,您可以使用以下指令禁用缓冲请求,该指令在 httpserverlocation 节内有效:

proxy_request_buffering off;

请注意,在某些情况下,如果您使用该功能,这将阻止故障转移到备份服务器。如果没有,你很好。

另见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_request_buffering

如果需要,您可以有选择地禁用请求缓冲,例如仅针对某些 locations 或仅针对 POST 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2021-10-28
    • 2023-03-22
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多