【问题标题】:Laravel - prevent file access outside of public while deployment is in subfolderLaravel - 部署在子文件夹中时防止公共之外的文件访问
【发布时间】:2021-09-06 21:47:05
【问题描述】:

我对 laravel 比较陌生,如果这是一个不好的做法,请原谅我。 我有一个godaddy 域,可以说example.com,它指向我的ec2 实例。 我有 2 个项目 example.com 网站和管理员。 所以我的文件夹结构是

  • /var/www/html
    • 网站
    • 管理员

我想将 example.com 指向 /website,所以我从下面的虚拟主机管理它

<VirtualHost *:443>
  ServerName example.com
  DocumentRoot "/var/www/html/website"
</VirtualHost>

这按预期工作。

现在我想通过example.com/admin访问管理面板
为此,我在 /website 作为指向 /admin 文件夹的管理员创建了符号链接。
通过上述方法,我可以使用 example.com/admin/public 访问管理员,这符合我的预期,因为所有用户都已经在使用 /public url。
现在的问题是,当我在没有公开的情况下访问example.com/admin/ 时,它会显示公开之外的所有文件,而且我可以使用 url 看到 .env 文件。我怎样才能避免这种情况?

【问题讨论】:

    标签: php laravel .htaccess laravel-8 vhosts


    【解决方案1】:

    好吧,我找到了一个很好的解决方案
    首先,我添加了.htaccess 文件,其中包含以下几行

    RewriteEngine On 
    RewriteRule ^ public [L]
    

    这会将所有内容重定向到 /public

    其次,为了额外的安全性,我使用限制访问 .env 文件

    <Files .env>
    order allow,deny
    Deny from all
    </Files>
    

    如果有的话,我仍然想提出一些建议。谢谢。

    【讨论】:

      【解决方案2】:

      在虚拟主机配置中,通过Options 指令限制列表

      DocumentRoot "/var/www/html/website"
      <Directory "/var/www/html/website">
          Options -Indexes +FollowSymLinks
          AllowOverride None
          Order allow,deny
          Allow from all
      </Directory>
      

      此外,在虚拟主机配置中,您也可以像这样限制单个文件的公共访问权限

      <Files composer.json>
          <IfVersion < 2.4>
              order allow,deny
              deny from all
          </IfVersion>
          <IfVersion >= 2.4>
              Require all denied
          </IfVersion>
      </Files>
      
      <Files .env>
          <IfVersion < 2.4>
              order allow,deny
              deny from all
          </IfVersion>
          <IfVersion >= 2.4>
              Require all denied
          </IfVersion>
      </Files>
      

      【讨论】:

      • 谢谢。 标签很有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2020-12-24
      相关资源
      最近更新 更多