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

标签 php nginx

我正在尝试设置我的 nginx 服务器来完成以下任务

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 下载 example.php,并且/example/给出 500 内部服务器错误。谁能帮我吗?

最佳答案

您的try_files应如下所示

try_files $uri $uri/ $uri.php;

除最后一个参数外的所有参数均在当前上下文中进行测试。因此,当您使用 $uri.php 并找到该文件时,当前上下文意味着将其作为静态文件提供服务,它确实这样做了。但对于最后一个参数,它将检查配置中的其他位置 block 。

编辑-1

您对 x.com/example/opens example.php in/ 的请求不是常见的请求,要使其正常工作有点棘手。当我们在 try_files 中有 $uri.php 时,它会将 url 更改为 x.com/example/.php。因此我们需要通过添加一个额外的 block 来额外处理这个 url

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

关于php - nginx try_files 下载 php 文件而不是加载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277971/

相关文章:

php - 从 php 嵌入变量

php - 如何使用条件语句将两个搜索结果语句合为一个?

amazon-web-services - 使用 Ingress 的 Kubernetes 上的 s3 代理

github - 为 nginx 用户添加 SSH-Key (for github)

php - 找出MYSQL数据库中记录了哪几个月的数据?

php - 多个 mysqli multi_query

docker - Kibana 5.5.1 位于 nginx 1.13 代理后面(dockerized)

php - nginx $_GET 问题

python - Nginx、Flask、Gunicorn 502 错误

php - 逃避 MySQL 查询问题