Nginx 身份验证请求意外状态 404

lea*_*ner 4 authentication runtime-error nginx http-status-code-404

当子请求发送到 nginx auth_request 模块时,我收到以下错误。作为参考,我开发了一个基于 Spring Security 的 Web 应用程序,其中仅包含一种 Web 方法,即通过身份验证来验证用户凭据和会话,并以 200 或 401 错误代码进行响应。

成功验证页面重定向到应显示上游应用程序主页的主页 url 后,我在 nginx 日志中收到此错误,其状态如下

错误 身份验证请求意外状态:404 发送到客户端时,客户端:127.0.0.1,服务器:,请求:“GET / HTTP/1.1”,主机:“localhost:8180”,引用者:“ https://localhost:8180/登录.html “*

此错误表明 nginx 已将完整的请求 url 传递给 auth 子请求,而不是“/authenticate”,并且 auth 服务会查找在其中找不到的资源并抛出 404 错误。

    server {
    listen    8180 ssl;
    ssl_certificate         certs/nginx.pem;
    ssl_certificate_key     certs/nginx.pem;

    location /{
        proxy_set_header xclnt "ap1";  
        auth_request /authenticate;
        proxy_pass https://adi-backend;         
    }

    location = /authenticate {
        proxy_set_header xclnt "ap1";  
        proxy_pass http://authserv;
    }

    location = /login.html {
        root   html;
    }

    location = /50x.html {
        root   html;
    }
    error_page   500 502 503 504  /50x.html;
    error_page  401 403 login.html;
}
Run Code Online (Sandbox Code Playgroud)

Kaj*_*nus 5

文档说不支持返回状态 404 Not Found from http://authservngx_http_auth_request_module只需要 2xx、401 或 403:

\n\n
\n

如果子请求返回2xx响应码,则表示允许访问。如果返回401或403,则表示访问被拒绝,并显示相应的错误代码。子请求返回的任何其他响应代码都被视为错误。

\n
\n\n

编写 auth 模块的人显然认为 404 意味着无法找到 auth 端点本身,例如,auth 请求发送到了错误的 URL。而不是所请求的东西不存在。

\n\n

因此,如果您不希望日志中出现这些错误,请使用 auth 模块以外的其他内容。...

\n\n

...也许是Lua?这就是我正在做的事情,当我希望“auth”处理程序既进行身份验证,又检查请求的内容是否存在时:

\n\n

这是我的 Lua 方法(效果很好):

\n\n
location ~ ^/-/u/([^/][^/]+/)(.*)$ {\n\n  # (1. There\'s Nginx\'s http_auth_request_module module, but it handles upstream 404 Not Found\n  # as an internal error. So it cannot both do auth, and check does-it-exist? at the same time.\n  # 2. ngx.location.capture is synchronous, but non-blocking: the code execution stops, until\n  # a response is received \xe2\x80\x94 but meanwhile, the nginx worker continues with other things.)\n  set $publSiteId $1;\n  set $hashPath $2;\n  access_by_lua \'\n    response = ngx.location.capture("/_auth_upload/" .. ngx.var.publSiteId .. "/" .. ngx.var.hashPath)\n    if response.status == 404 then\n      ngx.status = 404\n      ngx.say("Not found. [TyNGXFKB604]")\n      return ngx.exit(ngx.OK)\n    end\n    if response.status == 401 or response.status == 403 then\n      ngx.status = response.status\n      ngx.say("Access denied. [TyNGX5KWA2]")\n      return ngx.exit(ngx.OK)\n    end\';\n\n  alias /opt/talkyard/uploads/public/$2;\n  ... cache headers ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是 Nginx 子请求处理程序:

\n\n
location /_auth_upload/ {\n  # Only for Nginx subrequests.\n  internal;\n  proxy_pass              http://app:9000/-/auth-upload/;\n  proxy_pass_request_body off;\n  proxy_set_header        Content-Length "";\n  proxy_set_header        X-Original-URI $request_uri;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

完整源码在这里。我希望这有帮助 :- )

\n