在NGINX中使用Postgres Basic Auth将查询结果添加到代理HTTP头

我们正在尝试使用ngx_postgres模块通过NGINX进行基本authentication。 我们希望从查询结果对中检索一个值并将其添加到HTTP头,然后将其代理到另一个服务器。

我们已经成功通过postgres进行身份validation,但问题是已经将结果对传递给代理。 我们目前使用的代码如下:

location = /test_auth { internal; postgres_escape $user $remote_user; postgres_escape $pass $remote_passwd; postgres_pass geo_database; postgres_query "select user_name,contract_id,access_token from schema_name.table_name where user_name=$user and password=md5($pass);"; postgres_rewrite no_rows 401; more_set_headers -s 401 'WWW-Authenticate: Basic realm="Restricted"'; postgres_output none; postgres_set $query_val 0 0 required; } location /test/ { auth_request /test_auth; proxy_pass http://back_end_server/public-dev/; proxy_set_header test $query_val; proxy_redirect http://back_end_server/public-dev/ http://example_domain.com/; } 

这是我们尝试过的许多不同事物的一个子集。 在这个例子中的问题是,query_val似乎在子请求中被销毁。 当postgres调用不在子请求中时,我们可以成功地检索查询结果对,但是这会禁用我们的身份validation。

我们还在主要位置尝试了一个简单的查询,然后将这些数据代理到另一个位置,但是失败了。 如果我们将它定向到一个像index.html文件这样的本地资源,那么查询结果对就会被正确地添加到头文件中,但是只要我们试图将它添加到http头并发送给代理,就不会更久的存在。

任何意见在这个问题将不胜感激。

[更新]我花了整整一天的时间阅读有关我正在使用的模块,我真的认为这是模块阶段顺序的问题。 我有一种感觉,代理重写是在postgresauthentication子请求实际返回结果之前发生的。 我会继续调查。 任何帮助仍然感激。

这个问题的解决scheme最终在正确的NGINX阶段使用auth_request_set函数来提取正确的值。 以下代码是工作解决scheme。

 location = /test_auth { internal; postgres_escape $user $remote_user; postgres_escape $pass $remote_passwd; postgres_pass geo_database; postgres_query "select user_name,contract_id,access_token from schema_name.table_name where user_name=$user and password=md5($pass);"; postgres_rewrite no_rows 401; more_set_headers -s 401 'WWW-Authenticate: Basic realm="Restricted"'; postgres_output none; postgres_set $query_val 0 0 required; } location /test/ { auth_request /test_auth; auth_request_set $proper_query_val $query_val; proxy_pass http://back_end_server/public-dev/; proxy_set_header test $proper_query_val; proxy_redirect http://back_end_server/public-dev/ http://example_domain.com/; }