nginx、php 和包含 php 文件的 url

nginx、php 和包含 php 文件的 url

我刚刚安装了 moodle,它使用如下 URL 作为其主题:

http://moodle.example.com/theme/styles.php/standard/1380191797/all

nginx 没有意识到这是对 PHP 文件的调用,因此出现 404 错误。

我目前的重写技术主要围绕着挑选参考资料,/index.php这些参考资料可能看起来有些过时,但之前它已经在许多棘手的事情上发挥了作用。这是我目前的配置。

server {
        listen 80;
        server_name moodle.example.com;
        root   /websites/sbmoodle/moodle;
        index index.php;

        location / {
                try_files $uri $uri/ /index.php$request_uri /index.php;
        }

        location ~ \.php($|/) {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name$fastcgi_path_info;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
}

现在我在 Stack Exchange 的冷光下看,我感觉它就像末尾的 php 处理程序 ( \.php($|/))应该可以捕获这些错误。但是 nginx 仍然会抛出 404 错误。为什么?

是的,我已经测试过/theme/styles.php(没有遵循路径)并且它运行良好。

答案1

RTMFM,Oli。他们的维基百科上有正确的 nginx 配置看起来是有效的。以下是重点:

index index.html index.htm index.php;

location / {
        try_files $uri $uri/ /index.html;
}

location ~ \.php {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_param  PATH_INFO          $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;

    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  SCRIPT_FILENAME    $document_root$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  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx;

    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;

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
}

相关内容