我想配置nginx服务器,以便在链接中使用app.php
或app_dev.php
。现在没有他们,只有:
www.foobar.com/controller/method/param1/param2
默认情况下加载app.php。当我添加app.php作为前缀时也是如此,但当我添加app_dev.php时:
www.foobar.com/app_dev.php/controller/method/param1/param2
我收到错误,指出无法找到路线。因此它将app_dev.php视为控制器。为什么会这样?我怎样才能改变它?
配置文件是:
location /foobar/ {
index app_dev.php app.php
if (!-e $request_filename)
{
rewrite ^/(.*)$ /foobar/(app|app_dev).php?$1 last;
}
try_files $uri $uri/ app.php app_dev.php;
}
答案 0 :(得分:1)
只需使用下面的配置即可。这对app_dev.php和没有。
都有效location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
根据您的PHP-FPM配置,fastcgi_pass也可以是fastcgi_pass 127.0.0.1:9000。