nginx通过ip限制目录访问

我正在使用nginx,并想限制对除我以外的每个人的目录的访问。 我想在/限制访问PHP脚本。 到目前为止,我已经尝试了一些东西。 如果我记得,这阻止访问所有除了允许的IP,但所有的脚本被推送下载,而不是现在处理。

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

你需要第二个(我喜欢嵌套)的PHP块,因为你想这些PHP文件处理不同。 另外,假设/ restricted应该是一个uri前缀,而不仅仅是uri中的任何地方,你需要一个不同types的位置:

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