大网数据广告活动
服务器运维问答

nginx auth_request how to return backend status code

· 52CE 我爱测

when the backend proxy used in auth_request returns an error code different from 401 or 403, nginx is returning a 500 error code. The ngx_http_auth_request_module module (1.5.4+) implements client authorization based on the resul…

问题原文

原帖链接 · jobou · CC BY-SA 3.0

when the backend proxy used in auth_request returns an error code different from 401 or 403, nginx is returning a 500 error code.

The ngx_http_auth_request_module module (1.5.4+) implements client authorization based on the result of a subrequest. If the subrequest returns a 2xx response code, the access is allowed. If it returns 401 or 403, the access is denied with the corresponding error code. Any other response code returned by the subrequest is considered an error.

Is there a way to make nginx returns the status code from the backend and not 500 ?

已采纳解答

原帖链接 · jobou · CC BY-SA 3.0

I found a working solution. It uses the fact that auth_request will always return a 500 error code in case of a backend error different from 401 or 403 :

error_page 500 @process_backend_error;

location / {
    auth_request /auth
    auth_request_set $backend_status $upstream_status
}

location /auth {
    proxy_pass ...
}

location @process_backend_error {
    # here you have access to $backend_status which contains the returned status code from your autorization backend
}

Beware that the returned status code in $backend_status is a string.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。