当url中的.php时,Nginx想要执行一个文件

时间:2014-09-25 09:22:28

标签: nginx php

我最近从apache迁移到了nginx网络服务器。

我希望发生以下情况:

如果文件存在则返回内容 除了它的PHP文件然后传递给PHP 当404使用$ query_string

调用index.php时

例如:

example.com/jfkldsajlkfdsa =>返回内容index.php由php呈现

example.com/image.jpg =>返回内容

example.com/aphpfilethatexists.php =>返回内容由php呈现的aphpfilethatexists.php

example.com/aphpfilethatdoesntexists.php =>返回内容index.php由php呈现

现在情况如此:

example.com/jfkldsajlkfdsa =>返回内容index.php由php呈现

example.com/image.jpg =>返回内容

example.com/aphpfilethatexists.php =>返回内容由php呈现的aphpfilethatexists.php

example.com/aphpfilethatdoesntexists.php =>返回文件不存在的php错误File not found.

我使用以下配置:

server {
listen 80;
server_name example.com;
root /var/www/269;
index index.php;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass phpserver;
}

location ~ /.svn/ {
    deny all;
}
}

1 个答案:

答案 0 :(得分:0)

使用位置块内的!-e运算符检查是否存在文件/目录/符号链接(提供.php

location ~ \.php$ {
    if (!-e $request_filename) {
        rewrite ^ /index.php?$query_string break;
    }
    ....