PHP_SELF返回/index.php/index.php

时间:2017-03-12 18:08:14

标签: php nginx

为什么$_SERVER['PHP_SELF']会返回/index.php/index.php ??

请求

http://example.com

输出

/index.php/index.php

的index.php

<?php
echo $_SERVER['PHP_SELF'];

nginx.conf

server {
    listen  80;
    server_name  domain.com;

    root  /var/www/public/www;

    # Add trailing slash
    rewrite  ^([^.\?]*[^/])$ $1/ permanent;

    location / {
        try_files  $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include  /var/ini/nginx/fastcgi.conf;
        fastcgi_pass  php;
        fastcgi_param  SCRIPT_FILENAME /var/www/public/www/index.php;
    }
}

fastcgi.conf

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  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_script_name;
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;

2 个答案:

答案 0 :(得分:2)

当使用带有php-fpm的nginx时,这是已知问题(链接here):

  

将SCRIPT_NAME设置为某个内容时,$ _SERVER ['PHP_SELF']将包含   PHP_SELF环境值与SCRIPT_NAME的环境值连接在一起。

答案 1 :(得分:2)

我找到了一个有效的解决方案..

如果您更改了{ "types": { "customType1" : { "var1" : "string", "var2" : "int" } . . . }, "properties": { "prop1" : { "$ref" : "#/types/customType1" }, "prop2" : { "type" : "String", "value" : "www.google.com" } . . . } } 中的订单,那么fastcgi.confPHP_SELF

会返回正确的值

SCRIPT_NAME移到文件顶部

fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;