Nginx&处理不带扩展名的文件 [英] Nginx & Handling Files Without Extension

查看:181
本文介绍了Nginx&处理不带扩展名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Nginx处理不带扩展名的php文件(即处理 http://localhost/sample 以相同的方式处理 http://localhost/sample.php ).

I'm trying to get Nginx to handle php files without an extension (i.e. to handle http://localhost/sample the same way it would handle http://localhost/sample.php).

这是我的网站配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;

    root /var/www;
    index index.html index.php;

    location ~ \.(hh|php)$ {
        fastcgi_keep_conn on;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ $uri.html @extensionless =404;
    }

    location @extensionless {
        rewrite ^(.*)$ $1.php last;
    }
}

据我所知,它应该可以解决问题.但是,事实并非如此.尝试 http://localhost/sample 只会使我进入404页面(而

As far as I know it should do the trick. However - it doesn't. Trying http://localhost/sample just gets me to a 404 page (whereas http://localhost/sample.php works fine).

打开调试时,我在日志中看到以下内容:

When turning on debugging, I see the following in the logs:

2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "/sample" "/var/www/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use dir: "/sample" "/var/www/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script copy: ".html"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "/sample.html" "/var/www/sample.html"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "@extensionless" "/var/www@extensionless"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "=404" "/var/www=404"

这很奇怪.基本上,@ extensionless看起来像是纯文件名(而不是导致URL重写的位置).

Which is weird. It basically looks like @extensionless is treated as a plain filename (instead of a location leading to a rewrite of the URL).

我想念什么? :) 谢谢!

What am I missing? :) Thanks!

推荐答案

    try_files $uri $uri/ $uri.html @extensionless =404;

是的,@extensionless被视为普通文件,这是因为您在@extensionless之后添加了额外的=404 ="noreferrer"> try_files -@extensionless部分仅用作最后一个参数,作为内部重定向到另一个上下文的

Yes, @extensionless is treated like a normal file, and that's because you've added an extra =404 after @extensionless within try_files -- the @extensionless part would only work as the last parameter as an internal redirect to another context.

如果您不仅希望支持不带.php的处理请求,而且还希望从任何请求中剥离.php,则可能需要执行以下操作:

If you want not only to support handing requests without .php, but to also strip .php from any requests, you might want to do the following:

location / {
    if (-e $request_filename.php){
        rewrite ^/(.*)$ /$1.php;
    }
}
location ~ \.php$ {
    if ($request_uri ~ ^/([^?]*)\.php(\?.*)?$) {
        return 302 /$1$2;
    }
    fastcgi_...
}

这篇关于Nginx&处理不带扩展名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆