Http请求header中自定义的参数是如何传到PHP SERVER变量中的

salmonl 2018-10-26 04:39:40

1、问题描述:

用curl模拟一个请求,带上自定义的Header信息,如下,

curl 'niliu.me' -H 'x-forwarded-for:1.1.1.1' -H 'AA:aaaa' -H 'bb:bbbb'


在PHP文件中var_dump($_SREVER)输出如下部分信息:

["HTTP_BB"]=>
string(4) "bbbb"
["HTTP_AA"]=>
string(4) "aaaa"
["HTTP_X_FORWARDED_FOR"]=>
string(7) "1.1.1.1"



2、问题点:Nginx获取Http请求header中的参数,处理后存在nginx变量中($http_aa, $http_bb),如果显式的通过nginx fastcgi模块传给php-cgi, php-cgi处理后存在$_SERVR变量中,是很好理解的。问题是nginx fastcgi模块没有传的参数,php-cgi是如何接收到的呢(如果这里有表述不准确的地方,欢迎大家指正)

说明:显式传递如下, 实际并没有显式传递

cat nginx/conf/fastcgi_params
fastcgi_param HTTP_AA $http_aa;
fastcgi_param HTTP_BB $http_bb;
fastcgi_param HTTP_CC 'cc';



3、配置信息:
niliu.me博客所在的服务器是Nginx + PHP-FPM(版本:PHP 7.2.8; Nginx 1.14.0)的模式。

nginx conf中server块的定义如下:

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /dir/niliu$fastcgi_script_name;
include fastcgi_params;
}


fastcgi_params 内容如下:

cat fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

#fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
...全文
1269 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
salmonl 2018-10-29
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
贴大段英文咱也看不懂,估计你也是一知半解的

其实,php 只是从 CGI 环境变量中获取数据的


感谢版主的回答,确实这方面关联的知识还需要深入学习
xuzuning 2018-10-27
  • 打赏
  • 举报
回复
贴大段英文咱也看不懂,估计你也是一知半解的

其实,php 只是从 CGI 环境变量中获取数据的
salmonl 2018-10-27
  • 打赏
  • 举报
回复
注:以上Meta-variables的说明,参考文档http://www.faqs.org/rfcs/rfc3875.html
salmonl 2018-10-27
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
你开列的都不是客户端通过协议头传入的 通过 通过协议头传入的 参数名前都冠以 HTTP_ 前缀 如果你想深入了解,请搜索 CGI(通用网关接口)
查看了CGI文档,确实如您所说,通过协议头传入的参数名称加上HTTP_前缀存在Meta-variables中,PHP FastCGI直接从Meta-variables获取数据的话,REMOTE_ADDR这类非协议头传入的参数也在Meta-variables,REMOTE_ADDR却需要通过fastcgi_param映射(fastcgi_param REMOTE_ADDR $remote_addr;),测试不映射PHP中是输出不了。没有查到有资料明确说明HTTP_前缀的就不用映射

Meta-variables with names beginning with "HTTP_" contain values read
   from the client request header fields, if the protocol used is HTTP.
   The HTTP header field name is converted to upper case, has all
   occurrences of "-" replaced with "_" and has "HTTP_" prepended to
   give the meta-variable name.  The header data can be presented as
   sent by the client, or can be rewritten in ways which do not change
   its semantics.  If multiple header fields with the same field-name
   are received then the server MUST rewrite them as a single value
   having the same semantics.  Similarly, a header field that spans
   multiple lines MUST be merged onto a single line.  The server MUST,
   if necessary, change the representation of the data (for example, the
   character set) to be appropriate for a CGI meta-variable.
xuzuning 2018-10-26
  • 打赏
  • 举报
回复
你开列的都不是客户端通过协议头传入的
通过 通过协议头传入的 参数名前都冠以 HTTP_ 前缀

如果你想深入了解,请搜索 CGI(通用网关接口)
salmonl 2018-10-26
  • 打赏
  • 举报
回复
Google上查了很多信息,都没有说到如何传递过去的,欢迎大家留言讨论

21,894

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧