[英]NGINX - different backend proxy based on query parameter
我有一個特定的場景,我需要根據查詢參數路由到不同的后端:
https://edge1.cdn.com/file.zip?{secure_link}&{tokens}&route=aws1
aws1 的位置是http://edge1.amazonwebservices.com
如果它的 aws2 那么代理后端將是http://edge2.amazonwebservices.com
等等......但我仍然沒有想出如何做到這一點。
您可以使用map
指令從$arg_route
變量(包含route
查詢參數的值)中獲取代理主機名:
map $arg_route $aws {
aws1 edge1.amazonwebservices.com;
aws2 edge2.amazonwebservices.com;
...
default <default_hostname>;
}
server {
...
# if you want to proxy the request, you'd need a 'resolver' directive
resolver <some_working_DNS_server_address>;
location / {
# if you want to proxy the request
proxy_pass http://$aws;
# or if you want to redirect the request
rewrite ^ http://$aws$uri permanent;
}
}
如果您不想在沒有route
查詢參數的情況下提供請求,您可以省略map
塊中的最后一個default
行,並將以下if
塊添加到您的服務器配置中:
if ($aws = '') {
return 403; # HTTP 403 denied
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.