使用nginx + lua时无法访问客户端的错误响应体

我正在使用以下lua脚本转发从Node服务器返回的所有服务器响应。

对于/signup路由的错误处理如下:

if authenticationResult.status ~= 201 then
    ...

    ngx.status = authenticationResult.status
    ngx.say(authenticationResult.body)
    ngx.exit(401)
    return
end

客户端发送典型的注册请求,如下所示,使用superagent-promise库:

request
  .post(url)
  .type('form')
  .send(data)
  .end()
  .then((response) => {
    console.log('the response', response)
  })
  .catch((error) => {
    console.log('the error', error)
  })

当我从客户端发送有效的post请求时,.then中的response变量成功包含响应体。

但是,当我发送一个具有无效凭据的不当post请求时,_既不执行.then也不执行.catch_。取而代之的是,Chrome控制台立即显示POST http://api.dockerhost/signup 401 (Unauthorized)

我想知道我可以做些什么不同的事情来成功访问服务器的错误响应及其内容,而不仅仅是其状态码。

点赞
用户1580216
用户1580216

根据手册,如果你想让nginx在页面中返回内容,你需要使用 ngx.HTTP_OK 作为返回值。否则它将只返回 401 。

ngx.status = authenticationResult.status
ngx.say(authenticationResult.body)
ngx.exit(ngx.HTTP_OK)
return
2016-02-04 20:10:10