用nginx拒绝对所有文件的访问的正确方法是什么?

这是我的子域dev.mypage.com的configuration:

server { listen 80; server_name dev.mypage.com; root /home/user/web/dev; index index.html index.htm index.php; access_log /home/user/logs/dev/access.log; error_log /home/user/logs/dev/error.log; location / { try_files $uri $uri/ /index.html; allow xxx.xxx.xxx.xxx; deny all; } location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm/dev.sock; fastcgi_index index.php; include fastcgi_params; } } 

我想禁止访问/除我以外的所有文件和子目录 (xxx.xxx.xxx.xxx)。

运行这个configuration和

  1. 访问dev.mypage.com与其他IP变成403错误
  2. 与其他IP访问dev.mypage.com/subdir变成403错误
  3. 使用其他IP访问dev.mypage.com/index.php 返回页面
  4. 使用其他IP访问dev.mypage.com/subdir/index.php 返回页面

所以情况3和4不按预期工作。 我曾假设这一点

 location / {} 

将意味着所有的文件和目录。 我需要做些什么来阻止所有其他IP在这个子域中的所有内容?

我相信nginx会处理php地址块的php地址,并忽略根地址块。 我认为在使用deny all语句时,包含allow 127.0.0.1也是一个好习惯。

尝试添加到您的PHP位置块,作为前四个条目:

 try_files $uri =404; allow xxx.xxx.xxx.xxx; allow 127.0.0.1; deny all; 

编辑:我没有正确说明,至less根据我的理解,nginx将处理匹配请求的位置块。 所以对php地址的请求将根据该块的configuration进行处理。 同样,如果为特定的子目录创build了一个位置块,那么将应用这些规则而不是根目录位置块。