The*_*uck 0 proxy nginx oauth-2.0
我正在尝试访问位于OAuth2身份验证后面的媒体文件(图像,视频)。
为了访问资源,我需要向请求中添加自定义授权承载令牌,因此我不能使用简单的重写(至少据我所知)。
它不能通过普通的HTML(say img
或video
tag)来完成,所以我正在考虑让Nginx将查询代理到最终服务器。
每个媒体资源都将通过/proxy
具有token
参数(用于身份验证)和url
要加载的实际资源的路径加载。
范例网址:
http://myserver.com/proxy/?token=12345&url=http://protectedserver.com/custompath/asset
Run Code Online (Sandbox Code Playgroud)
这是我想到的,但是我不确定如何配置proxy_pass
指令,因为我需要它$url
专门代理变量。我不需要代理路径(无论如何它都是空的)。
location /proxy/ {
if ($arg_token ~ "^$") { return 404; }
if ($arg_url ~ "^$") { return 404; }
set $url $arg_url;
proxy_set_header Authorization "Bearer $arg_token";
set $args "";
#proxy_pass $url;
}
Run Code Online (Sandbox Code Playgroud)
注意:这将在封闭的环境中运行,并且只有特定的计算机(交互作用受限的信息亭)才能访问该页面,因此我不必担心auth令牌的潜在泄漏。
我在ServerFault上注意到了类似的问题,但没有人对此有答案:https : //serverfault.com/questions/671991/nginx-proxy-pass-url-from-get-argument
我正在寻找使它工作的配置设置或可行的替代解决方案。
这是针对我的问题的正确配置:
location /proxy/ {
if ($arg_token ~ "^$") { return 404; }
if ($arg_url ~ "^$") { return 404; }
set $url $arg_url;
set $token $arg_token;
set $args "";
# IMPORTANT, this is required when using dynamic proxy pass
# You can alternatively use any DNS resolver under your control
resolver 8.8.8.8;
proxy_pass $url;
proxy_set_header Authorization "Bearer $token";
proxy_redirect off;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4070 次 |
最近记录: |