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

标签 nginx basic-authentication urlencode url-encoding

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

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

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

如何强制 nginx 在设置授权 header 之前解码 auth 字符串?预先感谢您的帮助。

注意:由于某些特定于应用程序的限制,我无法首先在授权 header 中发送身份验证字符串。

最佳答案

“纯”nginx 解决方案

遗憾的是nginx并没有提供丰富的字符串操作集。我认为没有办法通过一些字符串进行全局搜索和替换(如果我们可以将所有 %2B 替换为 +,这可能是一个解决方案,%2F/%3D=)。然而,在某些情况下,nginx 会执行某些字符串的 url 解码 - 当此字符串成为将转发到上游代理服务器的 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;
    }
}

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

OpenResty/ngx_http_lua_module

这可以通过 openresty 轻松解决。或 ngx_http_lua_module使用 ngx.escape_uri功能:

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>;
    }
}

关于Nginx - 解码 URL 查询参数并将其作为请求 header 转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180469/

相关文章:

android - 在 Android 上,在不使用 UrlEncodedFormEntity 的情况下使用 URL 编码表单数据发出 POST 请求

linux - 如何从安装在 guest VM 上的主机访问 nginx 服务器?

dictionary - NGINX 中用户代理的不同文件

javascript - 在 Javascript 客户端中隐藏基本身份验证凭据

javascript - 我的 HTTP over AJAX 有什么问题 (javascript)

applescript - 我需要在 AppleScript 中对字符串进行 URL 编码

php - 如何设置权限以缓存和记录symfony docker容器

PHP 无法在带有 nginx 和 php-fpm 的 Amazon EC2 服务器上的页面内工作

repository - 使用基本身份验证设置私有(private) yum 存储库

javascript - 字符解码失败。值(value)被破坏