nginx - NGINX - 基于查询参数的不同后端代理
问题描述
我有一个特殊的场景,我需要根据查询参数路由到不同的后端:
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
查询参数的情况下为请求提供服务,则可以省略该块的最后default
一行map
并将以下if
块添加到您的服务器配置中:
if ($aws = '') {
return 403; # HTTP 403 denied
}
推荐阅读
- c++ - 如何在 Qt 中设置 XML 文件的编码?
- c# - 在 Controller/ViewComponent 中设置/更改属性的 Display(Name="") 属性
- java - 将元素动态添加到地图的地图Java 8
- android - Android在代码/动态中为Views设置不同的样式
- python - 打印二维列表的一部分
- sip - Restcomm USSD - 不返回对话框
- sparql - 将文档中的值绑定到 SPARQL 变量 (MarkLogic)
- java - IllegalArgumentException:没有为片段的 id (fragments_container) 找到视图
- sql - 如何在不重复整数的情况下随机创建 ID 列?
- php - 如何从 php pdo 中的 mssql 查询中获取受影响的行数