【问题标题】:Can not get headers and status code returned by auth_request无法获取 auth_request 返回的标头和状态码
【发布时间】:2021-10-13 13:33:04
【问题描述】:

我有以下测试nginx配置:

user  nginx;
worker_processes  auto;

error_log /dev/stderr debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /dev/stderr  main;

    sendfile        on;

    keepalive_timeout  65;

    upstream content {
      server 127.0.0.1:4001;
    }

    server {
        listen 4000;
        server_name test;
        gzip off;
        autoindex off;

        location /test-auth {
            add_header X-Test "testvalue";
            return 200;
        }

        location /proxy {
            add_header "X-Test1" "test1";
            auth_request /test-auth;
            auth_request_set $test $sent_http_x_test;
            auth_request_set $test2 $upstream_status;
            add_header X-Test $test;
            add_header X-Test2 $test2;
            proxy_pass http://content?test=$test&test2=$test2;
            proxy_pass_request_body off;
        }
    }

    server {
        listen 4001;
        add_header X-Test3 "test3";
        return 200 "testt response $args";
    }
}

我希望请求/proxy URL 将返回所有测试标头:X-Test(身份验证请求返回的标头)、X-Test1(只是一个示例)、X-Test2(身份验证请求 HTTP 状态的值)、X-Test3(由下游的content设置)。

但实际上,这个请求只返回X-Test1X-Test2。我无法通过auth_request_set 指令获得任何值(标题或返回状态)。我尝试了在 google 中找到的两种变量名称的变体:$sent_http_x_test$upstream_http_x_test,但没有运气。 $test 变量始终为空。

我看到以下官方示例:https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-subrequest-authentication/auth_request_set $test2 $upstream_status; 行也不起作用,$test2 变量始终为空。

我做错了什么?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    问题是/test-auth位置没有配置上游,你可以试试下面的配置。

    $upstream_http_x_test 是正确的。

    user  nginx;
    worker_processes  auto;
    
    error_log /dev/stderr debug;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        access_log  /dev/stderr  main;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        upstream content {
          server 127.0.0.1:4001;
        }
    
        server {
            listen 4000;
            server_name test;
            gzip off;
            autoindex off;
    
            location /real-auth {
                add_header X-Test "testvalue";
                return 200;
            }
            
            location /test-auth {
                proxy_pass http://127.0.0.1:4000/real-auth;
            }
    
            location /proxy {
                add_header "X-Test1" "test1";
                auth_request /test-auth;
                auth_request_set $test $upstream_http_x_test;
                auth_request_set $test2 $upstream_status;
                add_header X-Test $test;
                add_header X-Test2 $test2;
                proxy_pass http://content?test=$test&test2=$test2;
                proxy_pass_request_body off;
            }
        }
    
        server {
            listen 4001;
            add_header X-Test3 "test3";
            return 200 "testt response $args";
        }
    }
    

    【讨论】:

    • 为什么auth_request 需要proxy_pass 才能获取标头并返回状态?
    • 好像auth_request没有直接发送请求,看起来像是从/test-auth块读取配置。可以查看access.log,有请求发送到/test-auth,auth请求直接发送到/real-auth
    • 一位 nginx 贡献者帮助找出了根本原因:github.com/nginx/njs/issues/417#issuecomment-942494313 问题出在 ngx_http_headers_module 中。子请求“(r!= r->main)”的add_header被忽略,auth_request是一个子请求。这意味着 add_header 在直接访问位置与作为子请求时的工作方式不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多