【问题标题】:nginx friendly URIs with access deny on unfriendly requests对不友好的请求访问拒绝的 nginx 友好 URI
【发布时间】: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;
}

【问题讨论】:

  • 我的回答有什么问题吗,还是完全解决了您的问题?如果是后者,如果您能接受并投票,我将不胜感激。谢谢!

标签: linux security nginx


【解决方案1】:

这似乎是somewhat of a duplicate of your later question,但是,如果您仍然想解决这个问题,下面的代码应该做您想做的事情,而不是实际拒绝所有请求。

if ($request_uri ~ "^[^?]*?(/index(?:\.php)?|\.php)(?:\?.*)?$") {   return  403;    }

使用pcre (which is the library that nginx uses) 进行一些调试。

$ pcretest
PCRE version 8.30 2012-02-04

  re> #^[^?]*?(/index(?:\.php)?|\.php)(?:\?.*)?$#
data> /test
No match
data> /test.php
 0: /test.php
 1: .php
data> /index.php
 0: /index.php
 1: /index.php
data> /iindex.php
 0: /iindex.php
 1: .php
data>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 2017-05-18
    • 1970-01-01
    • 2013-06-02
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多