【发布时间】:2015-10-07 22:39:31
【问题描述】:
我目前有一个 nginx 配置,可以仅使用“/contact”来请求“/contact.php”等文件。而且我找到了将任何 .php 请求重定向到其友好对应方的解决方案,但是,我认为可能有更优雅的解决方案。
是否可以对“/articles/index.php”或“/articles/index”之类的 URI 发出 403 或 404 请求(请注意,友好的 URI 重写已启用)并且仅接受通过“/articles/”的请求" 仍然会加载 "/articles/index.php" 文件?
基本上,我希望任何目录中的任何“/index”或“/index.php”,或.php 请求到403 或404,并且只接受友好的无扩展名请求或目录根/ 来加载index.php(没有它在 URI 中被请求)。
这可能吗?我在我的配置中尝试了类似的方法来拒绝 .php 请求,但由于技术上存在重写,它只会拒绝所有请求。它目前不在那里,因为它不起作用。
当前配置:
location / {
try_files $uri $uri/ @extensionless-php;
index index.php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ /includes/(.+)\.php$ {
deny all;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
【问题讨论】:
-
我的回答有什么问题吗,还是完全解决了您的问题?如果是后者,如果您能接受并投票,我将不胜感激。谢谢!