Nginx - 在try_files中,php文件下载而不是执行

时间:2014-08-01 14:08:39

标签: php nginx

如果我请求/maintenance.php,它就会被执行。

如果我请求/ foo(和maintenance.php存在),则会下载maintenance.php。

我错过了什么?

location / {
    try_files $uri /maintenance.php @rewriteapp;
}
location @rewriteapp {
    rewrite ^(.*)$ /app_prod.php/$1 last;
}
location ~ ^/(app_prod|maintenance)\.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;
}

注意:好像是

location ~ ^/(app_prod|maintenance)\.php(/|$)

后不会触发

try_files $uri /maintenance.php @rewriteapp;

2 个答案:

答案 0 :(得分:3)

作为explained heretry_files本身不会为除最后一个参数之外的任何内容发出内部重定向。

因此,如果maintenance.php存在,

try_files $uri /maintenance.php @rewriteapp;

只会将文件作为纯文本提供或下载,具体取决于您的default_type配置设置。

解决方案

注意:根据文档,在这种情况下使用perfectly fine

if (-f $document_root/maintenance.php) {
    return 503;
}
error_page 503 @maintenance;
location @maintenance {
    rewrite ^(.*)$ /maintenance.php;
}

实际上,我想在维护页面中显示徽标,因此我最后添加了一些额外的行以允许资产投放。

if (-f $document_root/maintenance.php) {
    return 503;
}
error_page 503 @maintenance_or_assets;
location @maintenance_or_assets {
    try_files $uri @maintenance;
}
location @maintenance {
    rewrite ^(.*)$ /maintenance.php;
}

答案 1 :(得分:0)

编辑/etc/php5/fpm/pool.d/www.conf并查找以“listen =”开头的行。

确保将其设置为“/var/run/php5-fpm.sock”而不是127.0.0.1:9000。

如果设置为IP地址,请更改它并重新启动PHP。

相关问题