nginx try_files下载php文件而不是加载它

时间:2017-09-18 11:15:30

标签: php nginx

我试图设置我的nginx-server来完成以下

x.com opens index.php in /

x.com/example opens example.php in /

x.com/example/ opens example.php in /

x.com/foo/example opens example.php in /foo/

我有以下配置

server {
listen www.delibr.com:443 default_server;  # if this is not a default server, remove "default_server"
root /var/delibr.se;
index index.php index.html index.htm; # this is also irrelevant

server_name www.delibr.com;
ssl_certificate /path to/crt;
ssl_certificate_key /path to/key;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

location /beta {
    return 302 /#signup-form;
}

location / {
   try_files $uri $uri.php $uri/;
    if ($host != 'www.delibr.com') {
        rewrite     ^ https://www.delibr.com$request_uri? permanent;
    }
}

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    }

}

使用此设置/示例下载example.php和/ example /给出500内部服务器错误。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

try_files应如下所示

try_files $uri $uri/ $uri.php;

在当前上下文中测试除最后一个之外的所有参数。因此,当您使用$uri.php并找到该文件时,当前上下文旨在将其作为静态文件提供,它就是这样做的。但是对于最后一个参数,它将检查配置中的其他位置块。

修改-1

您对x.com/example/ opens example.php in /的请求并不常见,让它工作有点棘手。当我们在$uri.php中有try_files时,它会将网址更改为x.com/example/.php。所以我们需要通过添加一个额外的块来处理这个URL

location ~* /\.php {
   rewrite ^(.*)/\.php$ $1.php redirect;
}