【发布时间】:2020-04-17 00:51:42
【问题描述】:
我正在尝试让 Nginx 处理没有扩展名的 php 文件(即处理 http://localhost/sample 的方式与处理 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 页面(而 http://localhost/sample.php 工作正常)。
打开调试时,我在日志中看到以下内容:
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 重写的位置)。
我错过了什么? :) 谢谢!
【问题讨论】:
标签: nginx