![](/img/trans.png)
[英]Is it possible to set Brotli compression level dynamically based on the request headers in Nginx?
[英]Nginx create a map dynamically based on request/response values
我正在尋找一種方法來在 Nginx 語法的約束下實現我的以下目標。
由於各種原因,我需要 Nginx 來驗證從外部身份驗證系統返回的某些值(進一步處理)。 這似乎很容易通過使用 Nginx map 測試值來完成,但它似乎並不像我想象的那么簡單。
整個 nginx 和 auth 系統已經工作正常,我有我需要的 header 值。
目標:我唯一想做的就是根據一些響應 header 數據添加一個 map 的值。
Nginx map不能用在location/server塊中,只能用在http塊中。
當我使用nginx -t
測試以下配置時,它說:
nginx:[emerg] /etc/nginx/sites-enabled/test.conf:27 中不允許使用“map”指令
我意識到它在位置/服務器塊中是不允許的,但我不確定如何根據在服務器塊中運行的值創建 map。
我從現有工作配置中唯一改變的是添加map
本身和底部的if()
語句
站點配置文件:
server {
[...]
# Trigger an auth request
auth_request /auth;
# THIS VALUE MUST BE SET BEFORE MAKING THE MAP!!
auth_request_set $auth_resp_x_auth_user $upstream_http_x_auth_user;
location = /auth {
# stuff omitted
}
# Create a map based on the result of `auth_request_set` above
map $auth_resp_x_auth_user $is_invalid {
default "1";
tom "0";
olivia "0";
joe "0";
}
# stuff omitted
location / {
# Return 403 if the user is not valid
if ($is_invalid) {
return 403;
}
# stuff omitted
}
$auth_resp_x_auth_user
創建這個 map 值? 我認為您錯過了關於map
創建的變量的關鍵點:它們是延遲加載的。 因此,采用您的“理想配置”並簡單地將map
放入http {}
塊並沒有錯。
確實有一個問題:NGINX 將檢查變量是否在配置解析期間定義。 您可以嘗試像這樣進行虛擬定義:
http {
map $arg_dummy $auth_resp_x_auth_user {
default "1";
}
# Create a map based on the result of `auth_request_set` above
map "$auth_resp_x_auth_user" $is_invalid {
default "1";
tom "0";
olivia "0";
joe "0";
}
...
}
虛擬定義僅用於滿足 NGINX 配置檢查階段。 在運行時,它應該使用您通過auth_request_set
設置的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.