http header横杆被替换的原因

个人博客迁移

在用curl发送http请求的时候发现,自定义添加的header头会被特殊处理,比如  发送x-real-ip在server端接收会发现变为HTTP_X_REAL_IP,因为之前一直使用IIS,没遇到过这个问题,后来才知道这个是CGI的规范之一,如下所示http://www.ietf.org/rfc/rfc38754.1. 

Request Meta-VariablesMeta-variables contain data about the request passed from the serverto the script, and are accessed by the script in a system-definedmanner.Meta-variables are identified by case-insensitive names;there cannot be two different variables whose names differ in caseonly. Here they are shown using a canonical representation ofcapitals plus underscore ("_"). A particular system can define adifferent representation.4.1.18. Protocol-Specific Meta-VariablesThe server SHOULD set meta-variables specific to the protocol andscheme for the request. Interpretation of protocol-specificvariables depends on the protocol version in SERVER_PROTOCOL. Theserver MAY set a meta-variable with the name of the scheme to anon-NULL value if the scheme is not the same as the protocol. Thepresence of such a variable indicates to a script which scheme isused by the request.Meta-variables with names beginning with "HTTP_" contain values readfrom the client request header fields, if the protocol used is HTTP.The HTTP header field name is converted to upper case, has alloccurrences of "-" replaced with "_" and has "HTTP_" prepended togive the meta-variable name.The header data can be presented assent by the client, or can be rewritten in ways which do not changeits semantics. If multiple header fields with the same field-nameare received then the server MUST rewrite them as a single valuehaving the same semantics. Similarly, a header field that spansmultiple lines MUST be merged onto a single line. The server MUST,if necessary, change the representation of the data (for example, thecharacter set) to be appropriate for a CGI meta-variable.The server is not required to create meta-variables for all theheader fields that it receives. In particular, it SHOULD remove anyheader fields carrying authentication information, such as'Authorization'; or that are available to the script in othervariables, such as 'Content-Length' and 'Content-Type'. The serverMAY remove header fields that relate solely to client-sidecommunication issues, such as 'Connection'.如果是用nginx做web服务器,用户自定义的header,在带有下划线的情况下无法传递,因为在ngx_http_parse_header_line() 函数中

if (ch == '_') {

if (allow_underscores) {

hash = ngx_hash(hash, ch);

r->lowcase_header[i++] = ch;

i &= (NGX_HTTP_LC_HEADER_LEN - 1);

} else {

r->invalid_header = 1;

}break;}

nginx对headername的字符做了限制,默认 underscores_in_headers 为off,表示如果headername中包含下划线,则忽略掉。

 nginx中文档syntax: underscores_in_headers on | off;default:underscores_in_headers off;context: http, serverEnables or disables the use of underscores in client request header fields. When disabled, request header fields whose namescontain underscores are marked as invalid and are subject to the ignore_invalid_headers directive.Controls whether header fields with invalid names should be ignored. Valid names are composed of English letters, digits,hyphens, and possibly underscores (as controlled by theunderscores_in_headers directive).

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 8月28号, 在和所有国际车队失联整整一个月后, 瑞士队长马库斯终于回到了自己的老家。 当马库斯和他的副驾 开着老...
    宗毅大观阅读 375评论 0 1
  • 和泉守睁眼时,视线里仍然是熟悉的天花板,此时此身,心中生出的却是不一样的喜悦与满足。 他真的跟那人同处一室了,就像...
    小巫阅读 313评论 0 1
  • 公交车回到住处,前天读了点胡兰成,昨天读了两篇土家野夫。都是反复咀嚼,像没到烂熟的牛筋,像可以冲泡四遍五遍的茶叶。...
    镜中_阅读 236评论 0 0
  • 自从来到大学之后,总想着如何在茫茫信科院当中找到一些热爱文字的笔友。显然,我的愿望实现的可能性是寥寥。然而我在过去...
    Chosing_春幸阅读 466评论 0 2