Nginx - 解码 URL 查询参数并将其作为请求头转发

gio*_*b12 1 nginx urlencode basic-authentication url-encoding

我需要以user:pass查询参数 (es. http://example.com?BASIC_AUTH=dXNlcjpwYXNz)的形式向 nginx发送一些基本的身份验证凭据 (es. ),并能够以更常见的Authorization: Basic dXNlcjpwYXNz标头形式将它们转发到代理后面的目标服务器。

我已经能够使用正则表达式检索编码的 auth 字符串的值。问题是该值通常可能包含一些需要在 URL 中进行百分比编码的字符。埃斯。user:pass!->?BASIC_AUTH=dXNlcjpwYXNzIQ==变成?BASIC_AUTH=dXNlcjpwYXNzIQ%3D%3D

因此,当我将请求转发到目标服务器时,我最终Authorization: Basic dXNlcjpwYXNzIQ%3D%3D会指定目标服务器将拒绝哪个,并给出 401 Unauthorized。

如何在设置 Authorization 标头之前强制 nginx 解码 auth 字符串?在此先感谢您的帮助。

注意:由于某些特定于应用程序的限制,我无法首先在 Authorization 标头中发送 auth 字符串。

Iva*_*sky 5

“纯”nginx解决方案

不幸的是,nginx 没有提供丰富的字符串操作集。我认为没有办法做全局搜索和替换通过一些字符串(它可以是一个解决方案,如果我们能够取代所有%2B+%2F/%3D=)。但是,在某些情况下,nginx 会执行某个字符串的 urldecoding - 当该字符串成为将转发到上游代理服务器的 URI 的一部分时。

因此,我们可以将BASIC_AUTH请求参数的值添加到 URI 并向我们自己发出代理请求:

# Main server block
server {
    listen 80 default_server;
    ...

    location / {
        if ($arg_basic_auth) {
            # "basic_auth" request argument is present,
            # append "/decode_basic_auth/<BASE64_token>" to the URI
            # and go to the next location block
            rewrite ^(.*)$ /decode_basic_auth/$arg_basic_auth$1 last;
        }
        # No "basic_auth" request argument present,
        # can do a proxy call from here without setting authorization headers
        ...
    }

    location /decode_basic_auth/ {
        # This will be an internal location only
        internal;
        # Remove "basic_auth" request argument from the list of arguments
        if ($args ~* (.*)(^|&)basic_auth=[^&]*(\2|$)&?(.*)) {
            set $args $1$3$4;
        }
        # Some hostname for processing proxy subrequests
        proxy_set_header Host internal.basic.auth.localhost;
        # Do a subrequest to ourselfs, preserving other request arguments
        proxy_pass http://127.0.0.1$uri$is_args$args;
    }
}

# Additional server block for proxy subrequests processing
server {
    listen 80;
    server_name internal.basic.auth.localhost;

    # Got URI in form "/decode_basic_auth/<BASE64_token>/<Original_URI>"
    location ~ ^/decode_basic_auth/([^/]+)(/.*)$ {
        proxy_set_header Authorization "Basic $1";
        # Setup other HTTP headers here
        ...
        proxy_pass http://<upstream_server>$2$is_args$args;
    }

    # Do not serve other requests
    location / {
        return 444;
    }
}
Run Code Online (Sandbox Code Playgroud)

也许这不是一个非常优雅的解决方案,但它已经过测试并且有效。

OpenResty / ngx_http_lua_module

这可以通过使用ngx.escape_uri函数的openrestyngx_http_lua_module轻松解决:

server {
    listen 80 default_server;
    ...

    location / {
        set $auth $arg_basic_auth;
        if ($args ~* (.*)(^|&)basic_auth=[^&]*(\2|$)&?(.*)) {
            set $args $1$3$4;
        }
        rewrite_by_lua_block {
            ngx.var.auth = ngx.unescape_uri(ngx.var.auth)
        }
        proxy_set_header Authorization "Basic $auth";
        # Setup other HTTP headers here
        ...
        proxy_pass http://<upstream_server>;
    }
}
Run Code Online (Sandbox Code Playgroud)