網(wǎng)站首頁 編程語言 正文
一、獲取 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請求進行判斷,若是發(fā)現(xiàn)路徑中包含/timerbin/路徑時,對請求URL進行重寫,從新跳轉(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é)
原文鏈接:https://blog.csdn.net/JineD/article/details/125434338
相關(guān)推薦
- 2022-10-23 C#中使用Microsoft?Unity記錄日志_C#教程
- 2022-10-18 Go語言TCP從原理到代碼實現(xiàn)詳解_Golang
- 2022-05-23 Jenkins實現(xiàn)集群化管理以及流水線項目配置_nginx
- 2022-06-22 C++深入探究類與對象之對象模型與this指針使用方法_C 語言
- 2022-12-24 TS裝飾器bindThis優(yōu)雅實現(xiàn)React類組件中this綁定_React
- 2022-04-20 詳解C語言讀取文件求某一列的平均值_C 語言
- 2023-03-29 Pytorch中的數(shù)據(jù)轉(zhuǎn)換Transforms與DataLoader方式_python
- 2022-08-10 Oracle數(shù)據(jù)庫如何使用exp和imp方式導數(shù)據(jù)_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支