在 Azure Web 应用中使用重写规则获取权限错误


Getting permission error with rewrite rules within Azure web app

我有一个基于 Laravel 4 的 PHP 应用程序,我已经成功地在 Azure Web 应用程序中工作,我遇到的唯一问题是在尝试从example.azurewebsites.net/js/vendor/jquery.slicknav.min.js访问文件时,我收到"您无权查看此目录或页面"。响应。

文件夹site/wwwroot是我放置所有Laravel应用程序文件的地方,对于那些不熟悉Laravel的人来说,它包含四个文件夹:app,Bootstrap,public和vendor。

我在 Azure 门户中设置了虚拟应用程序和目录设置,以将/映射到site'wwwroot'public,并在公共目录中放置了一个包含以下内容的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
        <staticContent>
            <remove fileExtension=".svg" />
            <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
            <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
            <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
        </staticContent>
        <httpProtocol>
            <customHeaders>
                <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains"/>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type" />
                <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,DELETE,PUT,PATCH" />
            </customHeaders>
        </httpProtocol>
        <rewrite>
            <rules>
                <rule name="Laravel4" stopProcessing="true">
                    <match url="^" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>    
    </system.webServer>
</configuration>

通过此设置,重写将按预期工作,除非文件夹名称包括应用程序、引导程序、公共或供应商,就像 example.azurewebsites.net/js/vendor/jquery.slicknav.min.js 一样。

我的问题是如何修改重写规则来解决此问题?如果我通过在末尾添加连字符来重命名文件夹site'wwwroot'public'js'vendor',它将起作用。我还多次重新启动了容器。

编辑 为了更清楚起见,这是我的目录结构:

├─ wwwroot
│  └─ app
│  └─ bootstrap
│  ├─ public
│  │  └─ css
│  │  └─ files
│  │  └─ fonts
│  │  └─ imgs
│  │  ├─ js
│  │  │  └─ admin
│  │  │  ├─ app
│  │  │  │  └─ link-check.js
│  │  │  └─ old
│  │  │  ├─ vendor
│  │  │  │  └─ sortable.min.js
│  │  └─ less
│  │  └─ packages
│  │  └─ tmp
│  │  └─ uploads
│  └─ vendor
└─ ...

使用我的web.config配置link-check.js可以通过example.azurewebsites.net/js/app/link-check.js访问,而sortable.min.js则无法通过example.azurewebsites.net/js/vendor/sortable.min.js访问(并且/js/vendor路径中的其他任何内容都无法访问(。

注意:如果我将wwwroot'public'js'vendor重命名为 wwwroot'public'js'vendor-我可以从example.azurewebsites.net/js/vendor-/sortable.min.js查看sortable.min.js

事实证明,

路径中带有vendor的所有文件夹返回 403 禁止的原因是我安装了 Azure 作曲家 Web 应用扩展。在该扩展名的applicationHost.xdt文件中,它设置以下重写:

<rewrite xdt:Transform="InsertIfMissing">
  <rules xdt:Transform="InsertIfMissing">
    <rule name="RequestBlockingRule1" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{URL}" pattern="vendor/(.*)$" />
                </conditions>
                <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to     view this directory or page using the credentials that you supplied." />
            </rule>
  </rules>
</rewrite>

对我来说,删除扩展显然解决了问题,但是我有一种感觉,在我的重写规则部分添加<clear/>将清除扩展设置的规则并解决问题。

为了将来参考,这个问题在2015年11月7日和2015年7月23日的延期中提出。

下面显示了我修改后的web.config,它使用<clear/>来解决问题

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
        <staticContent>
            <remove fileExtension=".svg" />
            <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
            <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
            <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
        </staticContent>
        <httpProtocol>
            <customHeaders>
                <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains"/>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type" />
                <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,DELETE,PUT,PATCH" />
            </customHeaders>
        </httpProtocol>
        <rewrite>
            <rules>
                <clear/>
                <rule name="Laravel4" stopProcessing="true">
                    <match url="^" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>    
    </system.webServer>
</configuration>

您可以尝试以下步骤:

  • 虚拟应用程序和目录部分中的设置修改回将/映射到site'wwwroot'

  • public文件夹中的web.config移动到根目录,该目录可能是服务器中的site'wwwroot

  • web.config内容修改为以下内容:

<?xml><配置><重写><规则><规则名称 stopProcessing="true"><规则名称><规则名称 stopProcessing="true"><条件逻辑分组>

我在webconfig中添加了clear并为我工作。

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
         <clear/>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>