我有一个名为 admin
的文件夹位于 var/www/html
中目录。
我想从公共(public)互联网访问该文件夹,即只允许从本地主机访问。
为此
1) 创建了一个文件whitelist-admin
在 sites-available
目录并添加以下内容。
server{
location ~ /admin/.*\.php$ {
allow 127.0.0.1;
try_files $uri $uri/ /index.php;
deny all;
}
}
当我访问<public-ip>/admin
时我得到403
错误但链接 <public-ip>/admin/index.php
正在工作,我想禁用它。
最佳答案
server {
location = /admin { # its good to block the admin alone too
allow 127.0.0.1;
deny all;
try_files $uri $uri/ /index.php;
}
location ^~ /admin/ { # block anything beginning with /admin/
allow 127.0.0.1;
deny all;
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#fastcgi_index index.php;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
此配置应仅允许本地主机连接,并拒绝任何管理 URL 上的所有其他连接。
另一方面,如果您的计算机被阻止,可能是因为您通过公共(public) IP 地址而不是直接通过本地主机发出请求。(例如:如果您通过互联网进行重定向,您的 IP 地址将是服务器 IP,而不是本地主机)。
这应该可以解决问题,如果没有,也许有另一个位置可以捕获连接,而不是这个位置。如果有效或无效,请发表评论。
编辑:
^~ 将表明这是配置上最好的“~”,它将是“=”位置之后第一个被检查的。这应该优先考虑您的/admin/位置而不是 .php 位置。
这应该有效。 (最后:D)
关于php - nginx :allow folder access only from localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665806/