简体   繁体   English

Nginx上使用PHP的Apache样式“ MutliViews”

[英]Apache style “MutliViews” on nginx with php

I've looks up and down and, while this has been answered dozens of times, I can't get it to work. 我一直在向上和向下看,尽管已经回答了数十次,但我无法使它正常工作。 I'm trying to get apache style multiviews on my PHP site running under nginx. 我正在尝试在nginx下运行的PHP网站上获取apache样式的多视图。 In this case I don't care about all file extensions, just php. 在这种情况下,我不关心所有的文件扩展名,仅关心php。 So i have my try_files directive: 所以我有我的try_files指令:

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

which is all good and dandy, except that when I visit a PHP page without the PHP file extension, the PHP doesn't get rendered and just gets dumped straight to the browser. 这很好,而且很花哨,除了当我访问没有PHP文件扩展名的PHP页面时,PHP不会被渲染,而是直接转储到浏览器中。 I see why (PHP is only being used when the location ends in .php, but I've got no idea how to fix it. Here's my config: 我知道为什么(仅在位置以.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;
    }
}

In your scenario, the location / is the last processed location setting. 在您的方案中, location /是最后处理的位置设置。 Having a try_files on it won't make it past the location ~ ^(.+\\.php)$ setting (unless it ends with ".php"), therefore not being forwarded to the fastcgi upstream. 上面有一个try_files不会使其超出location ~ ^(.+\\.php)$设置(除非它以“ .php”结尾),因此不会转发到上游的fastcgi。 You might use a named location for that purpose (locations starting with "@"). 为此,您可以使用命名位置(以“ @”开头的位置)。

Here's an example based on your configuration: 这是一个基于您的配置的示例:

    # 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM