使用 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)

我想知道除了其状态代码之外,我可以做些什么来成功访问服务器的错误响应及其内容。

根据手册,如果您希望nginx将内容作为页面的一部分返回,则需要使用ngx.HTTP_OK作为返回。否则,它将只返回 401。

ngx.status = authenticationResult.status
ngx.say(authenticationResult.body)
ngx.exit(ngx.HTTP_OK)
return

相关内容

最新更新