1

我上下看了看,虽然这已经回答了几十次,但我无法让它发挥作用。我正在尝试在 nginx 下运行的 PHP 站点上获取 apache 样式的多视图。在这种情况下,我不关心所有文件扩展名,只关心 php。所以我有我的try_files指令:

try_files $uri $uri.php $uri/ $1?$args $1.php?$args

这一切都很好而且很花哨,除了当我访问没有 PHP 文件扩展名的 PHP 页面时,PHP 不会被渲染,而是直接转储到浏览器中。我明白了原因(仅当位置以 .php 结尾时才使用 PHP,但我不知道如何修复它。这是我的配置:

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    server_name inara.thefinn93.com;

    location /  {
            root /usr/share/nginx/www;
            try_files $uri $uri.php $uri/ $1?$args $1.php?$args;
    }

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

    location ~ /\.ht {
            deny all;
    }
}
4

1 回答 1

1

在您的场景中,这location /是最后处理的位置设置。有一个try_files它不会使它超过location ~ ^(.+\.php)$设置(除非它以“.php”结尾),因此不会被转发到上游的fastcgi。您可以为此目的使用命名位置(以“@”开头的位置)。

这是基于您的配置的示例:

    # real .php files only 
    location ~ ^(.+\.php)$ {
        # try_files is not needed here. The files will be checked at "location /"
        # try_files $uri =404;

        # do not split here -- multiviews will be handled by "location @php"
        # fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        # also not needed here/with try_files
        # fastcgi_index index.php;

        include fastcgi_params;
    }

    # pseudo-multiviews
    location @php {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;

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

        # search for the split path first
        # "$uri/" is not needed since you used the index
        try_files $fastcgi_script_name $uri.php =404;
    }

    # this should also be before "location /"
    location ~ /\.ht {
        deny all;
    }

    location /  {
        root /usr/share/nginx/www;
        # if file does not exist, see if the pseudo-multiviews work
        try_files $uri @php;
    }
于 2014-03-27T18:42:07.307 回答