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

Caching nginx auth_request

· 52CE 我爱测

I am trying to configure an nginx 1.12.2 reverse proxy to use auth_request and proxy_cache to authenticate requests to a microservice. End users send a simple Bearer token with their request, and the server tries to fetch a resou…

问题原文

原帖链接 · superstator · CC BY-SA 4.0

I am trying to configure an nginx 1.12.2 reverse proxy to use auth_request and proxy_cache to authenticate requests to a microservice. End users send a simple Bearer token with their request, and the server tries to fetch a resource with it to determine if they are allowed to proceed. That resource fetch should be cached for a given token to take load off the authentication endpoint. Here's my configuration so far:

ssl_certificate                 /etc/nginx/ssl/certificate;
ssl_certificate_key             /etc/nginx/ssl/key;
proxy_cache_path                /var/cache/nginx/auth_cache levels=1:2 keys_zone=auth_cache:1m max_size=1g inactive=60m;
server {
    listen                      80;
    listen                      443 ssl;
    server_name                 myapi.mydomain.com;
    location / {
        proxy_pass              http://myapi/;
        auth_request            /auth;
    }
    location = /auth {
        internal;
        proxy_pass              https://authendpoint.mydomain.com/api/user/verify;
        proxy_cache             auth_cache;
        proxy_cache_key         "$http_x_auth_token$request_uri";            
        proxy_pass_request_body off;
        proxy_set_header        Content-Length "";
        proxy_set_header        X-Original-URI $request_uri;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

From a client perspective this is working great, but when I look at the nginx server nothing ever gets written to the cache folder. The folder /var/cache/nginx/auth_cache gets created, but is always empty. What could I be missing?

update

Trying this with newer nginx (1.17.9, currently), we still never see anything written to the cache folder. Elsewhere I saw the suggestion that cookies could block caching - this app doesn't use cookies, but we tried anyway:

location = /auth {
    internal;
    proxy_pass              http://authservice/api/user/verify;
    proxy_cache             auth_cache;
    proxy_cache_key         "$http_x_auth_token$request_uri";   
    proxy_pass_request_body off;
    proxy_set_header        Content-Length "";
    proxy_set_header        X-Original-URI $request_uri;
    proxy_hide_header       Set-Cookie;
    proxy_ignore_headers    Set-Cookie;
    proxy_set_header        Cookie "";
}

Still the same behavoir; the auth_cache folder is created, but nothing is ever written there, and requests to our auth endpoint are not cached.

Just for reference, the results from our auth endpoint are extremely minimal:

< HTTP/1.1 200 OK
< Date: Sun, 12 Apr 2020 18:04:08 GMT
< Server: Kestrel
< Content-Length: 0

We've also simplified the remote auth endpoint since I originally asked this question. Now, it is a plain http resource running inside the same cluster as the nginx process. I've updated the example above to reflect that.

final update

On the off chance somebody else tries to use this as a reference, in addition to the two helpful answers below, we needed to fix the cache key in the example above:

proxy_cache_key         "$http_x_auth_token$request_uri";            

Only makes sense if you're actually using X-Auth-Token as a header and not just Authorization. The key above will work, but it means the first proxied auth response for any uri will be cached, and the actual token will be ignored. Really, we needed:

proxy_cache_key         "$http_authorization$request_uri";            

已采纳解答

原帖链接 · felixbuenemann · CC BY-SA 4.0

Try upgrading NGINX.

I had the same problem with 1.14.0, where manual requests to /auth with curl were cached, but auth_request sub-requests were not.

After upgrading to NGINX 1.16.1 it started to work.

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

资料来源与适用范围

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

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

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