【问题标题】:php 7 - "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest" - out of nowherephp 7 - “HTTP_X_REQUESTED_WITH” => “XMLHttpRequest” - 无处不在
【发布时间】:2015-09-30 22:32:04
【问题描述】:

我花了很多时间处理这个案子,但我不明白它是如何产生的。 所以。我有旧的 php 应用程序。 现在它正在以下环境中工作:

PHP 5.6.10-1+deb.sury.org~trusty+1 (cli) 
Zend Engine v2.6.0 with Zend OPcache v7.0.6-dev

nginx/1.4.6

为了测试我设置了 php-7

PHP 7.0.0RC3 (cli) 
Zend Engine v3.0.0-dev, with Zend OPcache v7.0.6-dev

nginx/1.4.6

所以,案例。 我第一次加载页面: http://storage3.static.itmages.com/i/15/0929/h_1443537572_8408994_b7e2156e5f.png

页面加载后,执行了几个ajax请求。

重新加载页面后,在 $_SERVER 变量中我们有 HTTP_X_REQUESTED_WITH http://storage3.static.itmages.com/i/15/0929/h_1443538095_6543758_9b4a478a13.png

但正如您在请求标头中看到的,我们没有此标头。 您可以在下面看到来自 nginx 的标头(访问日志)

Ajax 请求

10.0.2.2 - - [29/Sep/2015:15:00:44 +0300] "GET /stat/notifications-count HTTP/1.1" 200 30341 "http://vm-hub.dev:8888/client" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-" "no-store, no-cache, must-revalidate" "XMLHttpRequest"

正常请求

10.0.2.2 - - [29/Sep/2015:15:14:32 +0300] "GET /client HTTP/1.1" 200 95434 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-" "no-store, no-cache, must-revalidate" "-"

所以:浏览器不发送此标头。 Nginx 看不到也不会发送此标头。但是在 $_SERVER 中存在这个标头。

我创建了只转储 $_SERVER 变量的小型 php 脚本。在上述情况下,我从浏览器运行它 http://storage2.static.itmages.com/i/15/0929/h_1443538918_1985783_dcff1bacfb.png

并有 HTTP_X_REQUESTED_WITH 出现。

我无法定义 - 这是 php 错误还是其他软错误。

谁能给点建议?

也许 php 团队的某个人阅读了这个问题并可以评论这个案例?

PHP 从这里安装:https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0 以及一些手动编译的模块。

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    client_max_body_size 200M;


        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$sent_http_cache_control" "$http_x_requested_with"';



    sendfile on;
    keepalive_timeout 65;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

site.conf

server {
    listen 80;
    server_name vm-hub.dev www.vm-hub.dev;

    root /media/sf_data/projects/hub/html/hub/public;
    index index.php;

    log_not_found off;
    charset utf-8;

    error_log /media/sf_data/projects/hub/html/hub/log/error.log;
    access_log /media/sf_data/projects/hub/html/hub/log/access.log main;

    try_files $uri @rewriteapp;

    location = /favicon.ico { log_not_found off; access_log off; }

    location @rewriteapp {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME /media/sf_data/projects/hub/html/hub/public/index.php;
        fastcgi_param  HTTPS off;
        fastcgi_param  APPLICATION_ENV dev;
    }
}

重要提示:此应用程序在相同版本的 Ubuntu、nginx、mysql 上用于生产。仅 PHP 版本不同。 这就是为什么我认为 php 中的问题。

更新1。有关问题的更多详细信息 在应用程序中,我们有以下代码

if ($request->isXmlHttpRequest()) {
    return new JsonResponse([/*any data*/]);
} else {
    return new Response('tempalte render');
}

请求和响应 (JsonResponse) - 这是 Symfony HttpFoundation 组件。 所以,当我第一次加载页面时 - 呈现模板。但是当我重新加载页面时,请求定义为 AJAX,并返回 JsonResponse。 但这种行为是永久性的。现在我无法确定它何时出现的条件。

更新 2。 当我发现这个错误时,我会尝试:

  1. 重启 nginx - 没有任何改变。
  2. 重启 php-fpm - 错误消失

所以,一切都表明错误发生在php中。

更新 3 - 有罪。

我认为有罪的是 - fastcgi_finish_request(); 您可以在下面找到测试脚本来检查此行为。 我测试它:

  1. PHP7 RC3 - 存在错误
  2. PHP 5.6.13 - 没有错误

test script gist

【问题讨论】:

  • 你的浏览器也换了吗?
  • 该标头看起来不像是针对同一个 URL 的。请求 URL 是 http://vm-hub.dev:8888/client,但 SCRIPT_FILENAME/media/sf_data/project/hub/html/hub/public/index.php
  • 我很困惑?您的 ajax 请求正在发送标头?您的正常请求未发送标头。截图是ajax请求还是普通http请求?
  • honerlawd 所有截图 rom 正常请求。
  • @Dmitry 要将评论发送给某人,请在其姓名前加上@。只是正常输入名称(或粗体)不会通知他们评论。

标签: php ajax nginx


【解决方案1】:

这是 RC3 中的错误。 已在 RC4 中修复

https://bugs.php.net/bug.php?id=70607

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 2011-03-03
    • 2012-06-15
    • 2012-07-13
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多