You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

NGINX auth_request与return 204配合失效问题及配置需求

问题:NGINX /whoami端点未正确处理认证重定向,始终返回204

我需要配置一个NGINX端点/whoami,通过auth_request指令验证用户会话,认证通过后在响应头返回令牌且无响应体。目前//api端点都能正常工作:无有效会话时会重定向到授权页面,但/whoami无论会话是否有效始终返回204。

当前完整NGINX配置如下:

server {
    listen 80;
    server_name _;

    location /oauth2/ {                                                          
        proxy_pass       http://oauth2-proxy:4180;                               
        proxy_set_header Host                    $host;                          
        proxy_set_header X-Real-IP               $remote_addr;                   
        proxy_set_header X-Scheme                $scheme;                        
    } 

    location = /oauth2/auth {
        internal; 
        proxy_pass       http://oauth2-proxy:4180;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Scheme         $scheme;
        proxy_set_header Content-Length   "";
        proxy_pass_request_body           off;
    }

    location /whoami {
        auth_request /oauth2/auth; 

        if ($http_authorization = "") {
            error_page 401 = /oauth2/start;
        }

        auth_request_set $token  $upstream_http_x_auth_request_access_token;
        proxy_set_header X-Access-Token $token;
        add_header Access-Token $token;

        return 204;
    }

    location /api {
        auth_request /oauth2/auth; 

        if ($http_authorization = "") {
            error_page 401 = /oauth2/start;
        }

        auth_request_set $token  $upstream_http_x_auth_request_access_token;
        proxy_set_header X-Access-Token $token;
        auth_request_set $auth_cookie $upstream_http_set_cookie;
        add_header Set-Cookie $auth_cookie;

        proxy_http_version 1.1; 
        proxy_pass http://web-app:8080/; 
    }

    location / {
        auth_request /oauth2/auth; 

        if ($http_authorization = "") {
            error_page 401 = /oauth2/start;
        }

        root   /app/dist;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

需求

  • 未认证时,和//api一致,重定向到/oauth2/start
  • 已认证时,在响应头返回$upstream_http_x_auth_request_access_token
  • 返回无响应体的204或200状态码

解决方案

问题出在/whoami块的指令执行顺序:return 204会直接终止请求处理,导致auth_request返回401时的错误处理逻辑无法触发。同时原配置中的if判断没有必要,auth_request本身已经会处理认证状态。

修改后的/whoami配置如下:

location /whoami {
    auth_request /oauth2/auth;

    # 处理未认证场景,重定向到授权入口,和其他端点保持一致
    error_page 401 = /oauth2/start;

    # 从auth_request上游响应中提取认证令牌
    auth_request_set $token $upstream_http_x_auth_request_access_token;
    # 仅在认证通过时添加响应头
    add_header Access-Token $token;

    # 返回无响应体的204状态码
    return 204;
}

说明

  1. 移除了多余的if ($http_authorization = "")判断:auth_request会自动处理认证状态,当用户未认证时会返回401,由error_page触发重定向。
  2. 调整指令顺序:确保error_page先处理认证失败的情况,只有认证通过后才会执行auth_request_setadd_headerreturn指令。
  3. error_page 401 = /oauth2/start中的=符号用于保持重定向的状态码(默认302),和//api端点的行为一致。

内容的提问来源于stack exchange,提问作者Sergei

火山引擎 最新活动