网站首页 > 技术教程 正文
背景
在日常的开发中,我们有时候会在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-10 Nginx和Firewall都可以实现四层转发,你喜欢哪一个?
- 2024-09-14 NGINX 应用性能优化指南(第二部分):反向代理缓冲
- 2024-09-14 10《Nginx 入门教程》Nginx 的反向代理(上)
- 2024-09-14 Kubernetes中如何转发请求到集群外?
- 2024-09-14 Nginx 全面攻略:动静分离、压缩、缓存、黑白名单、跨域、高可用
- 2024-09-14 Portainer实用教程Portainer如何使用 Nginx 容器实现端口转发?
- 2024-09-14 Nginx TCP代理转发和负载均衡(nginx代理tcp转http)
- 2024-09-14 记一次nginx无法转发到后端的问题
- 2024-09-14 Nginx的请求数据处理流程(nginx的请求数据处理流程是什么)
- 2024-09-14 nginx使用(nginx使用的端口)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)