Nginx 和处理没有扩展名的文件
Nginx & Handling Files Without Extension
我正在尝试让 Nginx 处理 php 个没有扩展名的文件(即处理 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 ^(.*)$ .php last;
}
}
据我所知,它应该可以解决问题。然而 - 它没有。尝试 http://localhost/sample just gets me to a 404 page (whereas 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 重写的位置)。
我错过了什么? :)
谢谢!
只是更新(以防万一有人觉得有用)我终于让它工作了。
这是成功的配置:
server {
listen 80;
root /var/www;
index index.html index.htm index.php;
server_name localhost;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /.php;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
try_files $uri $uri/ $uri.html @extensionless =404;
是的,@extensionless
被视为普通文件,那是因为您在 try_files
中的 @extensionless
之后添加了额外的 =404
-- @extensionless
部分只能作为最后一个参数作为内部重定向到另一个上下文。
如果您不仅希望在没有 .php
的情况下支持处理请求,而且还希望从任何请求中删除 .php
,您可能需要执行以下操作:
location / {
if (-e $request_filename.php){
rewrite ^/(.*)$ /.php;
}
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php(\?.*)?$) {
return 302 /;
}
fastcgi_...
}
最好用最快最简单的方式避免使用慢重写和邪恶if
:
location ~ ^(.*)\.php$ # If PHP extension then 301 redirect to semantic URL
{
return 301 $scheme://$server_name$is_args$args;
}
location ~ ^/(.*)
{
try_files $uri.php @static; # If static, serve it in @static
include fastcgi_params; # if semantic, serve it here
fastcgi_param SCRIPT_FILENAME $document_root/.php;
}
location @static
{
try_files $uri =404;
}
我正在尝试让 Nginx 处理 php 个没有扩展名的文件(即处理 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 ^(.*)$ .php last;
}
}
据我所知,它应该可以解决问题。然而 - 它没有。尝试 http://localhost/sample just gets me to a 404 page (whereas 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 重写的位置)。
我错过了什么? :) 谢谢!
只是更新(以防万一有人觉得有用)我终于让它工作了。
这是成功的配置:
server {
listen 80;
root /var/www;
index index.html index.htm index.php;
server_name localhost;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /.php;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
try_files $uri $uri/ $uri.html @extensionless =404;
是的,@extensionless
被视为普通文件,那是因为您在 try_files
中的 @extensionless
之后添加了额外的 =404
-- @extensionless
部分只能作为最后一个参数作为内部重定向到另一个上下文。
如果您不仅希望在没有 .php
的情况下支持处理请求,而且还希望从任何请求中删除 .php
,您可能需要执行以下操作:
location / {
if (-e $request_filename.php){
rewrite ^/(.*)$ /.php;
}
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php(\?.*)?$) {
return 302 /;
}
fastcgi_...
}
最好用最快最简单的方式避免使用慢重写和邪恶if
:
location ~ ^(.*)\.php$ # If PHP extension then 301 redirect to semantic URL
{
return 301 $scheme://$server_name$is_args$args;
}
location ~ ^/(.*)
{
try_files $uri.php @static; # If static, serve it in @static
include fastcgi_params; # if semantic, serve it here
fastcgi_param SCRIPT_FILENAME $document_root/.php;
}
location @static
{
try_files $uri =404;
}