我有 HTTP API,它使用包含 UNIX 時間戳作為選擇器的 URL 提供一些歷史值。示例:http ://apiserver.org/api/1664269800/value
時間戳值必須是 10 分鐘粒度之一,例如:10:10:00、10:10:10 轉換為時間戳
我需要實作“最新”功能,
我有簡單的 nginx 配置,想要的功能是對“/proxyredir”的訪問將傳遞給http://apiserver.org/api/1664269800/value/,其中時間戳數是動態計算的。
location /proxyredir/ {
proxy_pass http://apiserver.org/api/1664269800/value/;
}
我需要一些魔法來動態計算時間戳值、獲取實際日期時間、截斷到 10 分鐘粒度并執行代理傳遞。
我研究了 nginx perl 模塊https://nginx.org/en/docs/http/ngx_http_perl_module.html?_ga=2.25112314.176064600.1664266760-2134269433.1664266760 但沒有成功。
請任何想法如何解決這個問題?
uj5u.com熱心網友回復:
使用 nginx perl 模塊解決了問題。訣竅是 set_perl 指令必須在配置頂層“http”中定義,但如果在“位置背景關系”中使用此變數,則會在每個請求上重新評估
配置如下:
http {
...
perl_set $tstamptrunc '
sub {
my $r = shift;
# UNIX TIMESTAMP trucated to 10minutes
$epoch_10m_trunc = int(time()/600)*600;
return "$epoch_10m_trunc";
}
';
...
location ~ /api/latest/(. ) {
resolver 8.8.8.8;
proxy_pass: https://remote.org/api/$tstamptrunc/$1;
}
}
如果 proxy_pass 定義是動態定義的,則決議器定義是必需的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/513191.html
標籤:perlnginx
上一篇:Perl變數名前的*是什么意思?
下一篇:在perl中使用變數呼叫sed
