国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Nginx如何獲取自定義請求header頭和URL參數(shù)詳解

瀏覽:135日期:2023-03-13 15:37:11
目錄
  • 一、獲取 header 請求頭
  • 二、獲取url參數(shù)
  • 總結(jié)

一、獲取 header 請求頭

在 ngx_lua 中訪問 Nginx 內(nèi)置變量 ngx.var.http_HEADER 即可獲得請求頭HEADER的內(nèi)容。

在 nginx配置中,通過$http_HEADER 即可獲得請求頭HEADER的內(nèi)容。

案例:

$.ajax({
    .......
    headers: { 
Accept: "application/json; charset=utf-8", 
X-TimerLocal: "Bin"  //這個是自定義的請求頭
    },
    .......
});

在nginx的location配置中,在獲取header配置時,須要在header名稱前面加上固定前綴“http_“,并將header名稱中的“-”中劃線變?yōu)橄聞澗€,舉例說明:

       自定義的header名稱為X-TimerLocal,那在nginx中使用$http_x_timerlocal 來獲取到X-TimerLocal的值。

if ($http_x_timerlocal  = "BIN") {
    rewrite ^(.*/timerbin/.*)$ https://$host$1 permanent;
}

包含X-TimerLocal=BIN的header請求進(jìn)行判斷,若是發(fā)現(xiàn)路徑中包含/timerbin/路徑時,對請求URL進(jìn)行重寫,從新跳轉(zhuǎn)。

二、獲取url參數(shù)

在 ngx_lua 中訪問 Nginx 內(nèi)置變量 ngx.var.arg_PARAMETER 即可獲得GET參數(shù)PARAMETER的內(nèi)容。

在 nginx配置中,通過$arg_PARAMETER 即可獲得GET參數(shù)PARAMETER的內(nèi)容。

案例:

通過 http://www.test.com?name=hello&id=123 來驗證url的請求參數(shù),能夠在nginx中獲取到,只需要修改nginx.conf 配置文件如下,就可以在access.log中看到id和name在log中

http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;


? ? log_format main "{ "@timestamp": "$time_iso8601", "
? ? ""servername": "$http_host", "
? ? ""id": "$arg_id","
? ? ""name": "$arg_name","
? ? ""remote_addr": "$remote_addr","
? ? ""referer": "$http_referer","
? ? ""request": "$request","
? ? ""request_time": "$request_time","
? ? ""status": $status,"
? ? ""bytes":$body_bytes_sent,"
? ? ""agent": "$http_user_agent","
? ? ""x_forwarded": "$http_x_forwarded_for","
? ? ""upstr_addr": "$upstream_addr","
? ? ""upstr_host": "$upstream_http_host","
? ? ""ups_resp_time": "$upstream_response_time" }";


? ? access_log ?logs/access.log main;
? ? server_names_hash_bucket_size 128;

總結(jié)

到此這篇關(guān)于Nginx如何獲取自定義請求header頭和URL參數(shù)的文章就介紹到這了,更多相關(guān)Nginx自定義請求header頭和URL參數(shù)內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: Nginx
主站蜘蛛池模板: 伊吾县| 门源| 沙河市| 肃南| 江口县| 来安县| 尖扎县| 上虞市| 白山市| 永定县| 利川市| 克东县| 安吉县| 商都县| 昌乐县| 梨树县| 自贡市| 清流县| 安塞县| 航空| 浠水县| 田阳县| 乌苏市| 阿拉尔市| 新乡县| 克拉玛依市| 潜山县| 岳阳市| 锡林浩特市| 江西省| 苗栗市| 汝城县| 松桃| 榆中县| 德安县| 东乡| 会昌县| 高安市| 弥勒县| 雅安市| 称多县|