【问题标题】:Using IIS Url Rewrite Module To Point To Azure CDN使用 IIS URL 重写模块指向 Azure CDN
【发布时间】:2014-04-08 17:20:31
【问题描述】:

我在运行 Windows Server 2012 R2 的 Web 服务器上安装了 URL 重写模块,其 URL 如下:http://staging.mysite.net/

我将图像、字体、脚本等加载到 Azure CDN。

我在我的 Web.config 中指定了以下规则:

<rewrite>
    <rules>
        <rule name="CDN: fonts" stopProcessing="true">
            <match url="^fonts/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/fonts/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: images" stopProcessing="true">
            <match url="^images/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/images/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: pdf" stopProcessing="true">
            <match url="^pdf/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/pdf/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: scripts" stopProcessing="true">
            <match url="^scripts/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/scripts/{R:1}" appendQueryString="true" />
        </rule>
        <rule name="CDN: style" stopProcessing="true">
            <match url="^style/(.*)" />
            <action type="Rewrite" url="http://cdn.staging.mysite.net/style/{R:1}" appendQueryString="true" />
        </rule>
    </rules>
</rewrite>

即使文件存在,所有内容都作为 404 错误返回。例如,我的页面引用了 /images/en-us/logo.png。我验证了 http://cdn.staging.mysite.net/images/en-us/logo.png 确实存在。

此外,Url Rewrite snap in 的“测试模式”功能显示,当您输入 images/en-us/logo.png 时,{R1} 返回 en-us/logo.png em> 或 /images/en-us/logo.png 进入“Input data to test”字段。

我暂时尝试过使用 type="Redirect" 而不是 type="Rewrite" 但这会导致来自服务器的重复请求/响应。第一个是 301,第二个是 200。我只想要 200 响应。

++ 显然,mysite.net 并不是我真正的域,仅用于示例目的。

【问题讨论】:

    标签: iis azure url-rewrite-module azure-cdn


    【解决方案1】:

    我正在使用以下规则重写我的/assets 文件夹下的所有请求:

    <rewrite>
      <rules>
        <rule name="Rewrite to CDN">
          <match url="^assets/(.+)$" />
          <action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
        </rule>
       </rules>
    </rewrite>
    

    【讨论】:

    • 很奇怪,我真正看到的唯一不同是您没有使用 stopProcessing="true" 或 appendQueryString="true"。今晚我将尝试从我的规则中删除这些内容,看看是否可以解决问题。
    • 还使用稍微不同的正则表达式匹配 URL,并在重写 URL {R:0} 上而不是 {R:1}
    • 改变了我的风格以匹配你的风格,删除了 stopProcessing="true",删除了 appendQueryString="true",更改了正则表达式以匹配你的模式并将 URL 切换为像你的一样(没有文件夹名称并使用 {R :0})。还是没有运气!它看起来仍然无法正常工作。
    • 正在使用应用程序请求路由模块吗?我刚刚找到了这个答案:stackoverflow.com/questions/14269149/…
    • 是的,使用应用程序请求路由,我忘记了“启用代理”默认是禁用的,一旦启用它,它应该立即工作。
    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 2011-12-04
    • 2012-06-01
    • 2019-06-06
    • 2011-01-16
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多