php - nginx :allow folder access only from localhost

标签 php .htaccess ubuntu nginx

我有一个名为 admin 的文件夹位于 var/www/html 中目录。

我想从公共(public)互联网访问该文件夹,即只允许从本地主机访问。

为此

1) 创建了一个文件whitelist-adminsites-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,而不是本地主机)。

这应该可以解决问题,如果没有,也许有另一个位置可以捕获连接,而不是这个位置。如果有效或无效,请发表评论。

编辑:

理解来源:https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

^~ 将表明这是配置上最好的“~”,它将是“=”位置之后第一个被检查的。这应该优先考虑您的/admin/位置而不是 .php 位置。

这应该有效。 (最后:D)

关于php - nginx :allow folder access only from localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665806/

相关文章:

javascript - 使用 ajax 在服务器端接收 json 时出错

php - 从子文件夹调用数据库播种机

php - 如何将复选框值保存到数据库中

ajax - htaccess 将 AJAX url 重写为漂亮的链接

c++ - 如何使二进制文件与不同的发行版兼容

php清理输出表单数组

php - 如何从 UBUNTU 中的 codeigniter 中删除 index.php

php - 重写规则不允许我访问我域的 index.php

Java 已安装,在 list 中,但执行产生 "./java: No such file or directory"

ubuntu - ubuntu 上的 VIM 日晒配色方案