网站首页 > 技术文章 正文
背景
在日常的开发中,我们有时候会在http请求头中设置一些自定义的字段,比如这种格式x_token, x_ticket, x_uid, x_version,app_id等,这些字段中都含有下划线。 但后端程序获取头部信息时,会取不到对应的值。
场景复现x_test 去哪儿了?
www@SD-20200928IIIF:~$ curl --location --request GET 'http://localhost/test/header/get-header.php' \
> --header 'x_test: xxxxx' \
> --header 'y-test: yyyyy'
{
"post": [],
"header": {
"y-test": "yyyyy",
"user-agent": "curl\/7.68.0"
}
}
原因:
其实nginx对header name的字符做了限制,默认 underscores_in_headers 为off。
当是off的时候,忽略在客户端请求头字段中设置的下划线参数。名称包含下划线的请求标头字段将被标记为无效,并受ignore_invalid_headers指令的约束 。
之所以为off,其实官方也是不推荐采用下划线方式的。
解决办法
方法一:
header中自定义变量名时不要用下划线
个人比较推荐这种方式。常见的header变量都是遵循这种方式,例如:Content-Type,Content-Length,Accept-Ranges等。
方法二:
在nginx.conf中加上underscores_in_headers on配置
http {
...
underscores_in_headers on;
}
猜你喜欢
- 2024-10-23 微软发布6月Win11累积更新KB5039212/KB5039213
- 2024-10-23 快速体验之《gor+diffy实现线上流量复制到测试环境》
- 2024-10-23 Colbie Caillat: Try(colbiecaillattry歌词)
- 2024-10-23 基于阿里云 ASK 的 Istio 微服务应用部署初探
- 2024-10-23 浅谈ElasticSearch 集群部署(elastic集群配置)
- 2024-10-23 Python项目中跟踪系统导入Zipkin(基于python的目标跟踪算法)
- 2024-10-23 JVM参数及调优(jvm调优常用参数)
- 2024-10-23 Elasticsearch的路由routing的应用技巧
- 2024-10-23 如何将Elasticsearch的快照备份至OSS
- 2024-10-23 利用工具curl来查看http请求和https请求
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)