【问题标题】:Only allow script access to a file iis7.5iis7.5 只允许脚本访问文件
【发布时间】:2012-07-18 00:57:50
【问题描述】:

我的 iis7.5 服务器上有一个文件(称为“log.html”),我希望我的 PHP 安装能够访问和写入,但我不希望任何人直接访问该文件,因为例如输入“http://computername/log.html”(我在局域网上)。

如何阻止用户访问但允许 php 看到它?

使用下面建议的 web.config 文件时,我收到此错误:

【问题讨论】:

  • 我应该在服务器故障时问这个吗?

标签: windows iis iis-7.5 file-permissions


【解决方案1】:

您可以使用 IIS URL Rewrite 并创建一个请求阻止规则来阻止通过 HTTP 的访问:

例如:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="BlockLogFile" 
              patternSyntax="Wildcard" 
              stopProcessing="true">
          <match url="*" />
          <conditions>
            <add input="{URL}" pattern="/log.html" />
          </conditions>
          <action type="CustomResponse" 
                  statusCode="403" 
                  statusReason="Forbidden: Access is denied." 
                  statusDescription="This is sekret!" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

【讨论】:

  • 由于某种原因,当我使用您的 web.config 示例时,我收到一个内部服务器错误 (500.19),告诉我读取 web.config 文件时出现问题...
  • @starbeamrainbowlabs - 抱歉,从未注意到您的回复。配置示例是直接从我测试的服务器复制的。你能贴一张 500.19 页面的截图吗,它通常信息量很大。
  • 可以,但是如何在stackoverflow上显示呢?
  • @starbeamrainbowlabs 编辑器提供了一个图片上传按钮。如果你不能让它工作,那么你可以上传到外部图片托管商(imgur、imageshack 或任何地方),提供一个链接,我会为你导入它。
  • @starbeamrainbowlabs - 我认为您收到该错误是因为未安装/未安装 IIS7 URL 重写模块。
【解决方案2】:

我想我可能已经回答了我自己的问题!我仍然需要对其进行更多测试,也许如果它不能完全工作,那么有人可以纠正我:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="cache-control" value="no-store, no-cache, must-revalidate, max-age=0" />
            </customHeaders>
        </httpProtocol>
        <staticContent>
            <clientCache cacheControlMode="DisableCache" />
        </staticContent>
        <security>
            <requestFiltering>
                <denyUrlSequences>
                    <add sequence="log.html" />
                </denyUrlSequences>
            </requestFiltering>
        </security> 
    </system.webServer>
</configuration>

缓存控制位防止浏览器缓存它返回的任何内容。

希望这对其他人有所帮助!我对此还是很陌生,所以你也许可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多