nginx上的Apache风格的“MutliViews”,使用php

我已经上下查看,尽管这已经被回答了几十次,但我无法让它正常工作。我试图在我的PHP网站上运行nginx下获得apache样式多视图。在这种情况下,我不关心所有的文件扩展名,只是PHP。所以,我有我的try_files指令:nginx上的Apache风格的“MutliViews”,使用php

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;

}

}

回答:

在你的情况下,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;

}

以上是 nginx上的Apache风格的“MutliViews”,使用php 的全部内容, 来源链接: utcz.com/qa/263355.html

回到顶部