Security

阻止訪問站點中的每個文件,但 Nginx 中的 index.htm

  • October 20, 2011

我有一個只有一個文件的站點:index.htm其中有一些連結到 php 文件的 ajax。我想讓這些 php 文件只能通過來自該索引文件的 ajax(post 和 get)訪問,並阻止訪問除index.htm. 在 Nginx 中可以嗎?謝謝。

兩個想法:

最後,有人將能夠通過偽造標題直接找到它們,但這只是一個開始。

像這樣的東西會起作用:

   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;
   }

Buf 請記住,很容易欺騙此標頭,例如:

$ 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

引用自:https://serverfault.com/questions/237059