nginx 通过ip限制目录访问

Poe*_*Poe 6 nginx

我正在使用 nginx 并希望将目录的访问权限限制为除我以外的所有人。我想访问 /restricted 中的 php 脚本。到目前为止,我已经尝试了一些东西。如果我记得,这可以阻止访问除允许的 IP 之外的所有内容,但所有脚本都被推送到下载而不是现在处理。

location ~ /restricted { allow 1.2.3.4; deny all; }

kol*_*ack 8

您将需要第二个(我更喜欢嵌套的)php 块,因为您希望以不同方式处理这些 php 文件。此外,假设 /restricted 应该是一个 uri 前缀,而不仅仅是出现在 uri 中的任何地方,您需要不同类型的位置:

# This handles everything that starts with /restricted,
# and no regex locations will override it
location ^~ /restricted {
  allow 1.2.3.4;
  deny all;

  # This will inherit the allow/deny from the outer location
  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass backend;
  }
}
Run Code Online (Sandbox Code Playgroud)