簡體   English   中英

NGINX - 基於查詢參數的不同后端代理

[英]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
}

如果你需要代理你還需要一個請求resolver指令(你可以閱讀關於它的一些技術細節文章)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM