5

我有一个基于 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>

使用此设置,重写按预期工作,除非文件夹名称包含 app、Bootstrap、public 或 vendor,因为它与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\vendorwwwroot\public\js\vendor-可以查看sortable.min.jsexample.azurewebsites.net/js/vendor-/sortable.min.js

4

3 回答 3

10

It turns out the reason for all folders with vendor in the path returning a 403 forbidden was that I had the Azure composer web app extension installed. Within the applicationHost.xdt file for that extension it sets the following rewrite:

<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>

For me removing the extension obviously fixed the problem, however I have a feeling that adding a <clear/> to my rewrite rules section would clear the rules set by the extension and fix the issue.

For future reference this issue was raised against the extension on 7th Nov 2015 and on the 23rd July 2015.

Below shows my amended web.config that uses <clear/> to fix the problem

<?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>
于 2016-02-04T10:21:28.823 回答
2

您可以尝试以下步骤:

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

  • 将您web.configpublic文件夹移动到可能site\wwwroot在您的服务器中的根目录。

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

<?xml 版本="1.0" 编码="UTF-8"?>
<配置>
    <system.webServer>
        <重写>
            <规则>
                <rule name="重写对资产的路由访问(img, css, files, js, favicon)"
                  stopProcessing="true">
                    <match url="^(img|css|files|js|favicon.ico)(.*)$" />
                </规则>
                <rule name="导入规则 1">
                    <match url="^(.*)$" ignoreCase="false" />
                    <action type="重写" url="/public/{R:1}" />
                </规则>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <添加输入="{REQUEST_FILENAME}"
                                matchType="IsFile"
                                忽略大小写=“假”
                                否定=“真” />
                        <添加输入="{REQUEST_FILENAME}"
                                matchType="IsDirectory"
                                忽略大小写=“假”
                                否定=“真” />
                    </条件>
                    <action type="重写" url="public/index.php/{R:1}" />
                </规则>
            </规则>
        </重写>
    </system.webServer>
</配置>
于 2016-02-04T07:26:51.110 回答
1

我在 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>
于 2021-08-17T13:13:51.487 回答