在 Nginx 中阻止访问站点中除 index.htm 之外的每个文件

在 Nginx 中阻止访问站点中除 index.htm 之外的每个文件

我有一个只有一个文件的网站:index.htm其中有一些链接到 php 文件的 ajax。我想让这些 php 文件只能通过来自此索引文件的 ajax(post 和 get)访问,并阻止对除 之外的所有文件的访问index.htm。在 Nginx 中可以实现吗?谢谢。

答案1

两个想法:

最后,有人将能够通过伪造标头直接获得它们,但这只是一个开始。

答案2

类似这样的事情会起作用:

    location ~ \.php$ {
        if ($http_referer !~* index\.htm) {
            return   403;
        }

        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;

        fastcgi_intercept_errors        on;
        error_page 404 /error/404.php;
    }

但请记住,这个标题很容易被欺骗,例如:

$ telnet localhost 81
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /pwd.php HTTP/1.0

HTTP/1.1 403 Forbidden
Server: nginx/1.0.5
Date: Thu, 20 Oct 2011 03:06:03 GMT
Content-Type: text/html
Content-Length: 168
Connection: close

$ telnet localhost 81
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /pwd.php HTTP/1.0
Referer: index.htm

HTTP/1.1 200 OK
Server: nginx/1.0.5
Date: Thu, 20 Oct 2011 03:06:38 GMT
Content-Type: text/html
Connection: close

/var/www/localhost/htdocs

$ curl localhost:81/pwd.php
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.0.5</center>
</body>
</html>

$ curl --referer index.htm localhost:81/pwd.php
/var/www/localhost/htdocs

相关内容