使用Azure Web App上的web.config将子域重写到目录

问题描述 投票:0回答:1

我试图在我的网络服务器上的相应文件夹中托管我的所有子域名

https://foo.bar.com/index.html

位于

https://bar.com/foo/index.html

通过我当前的配置,文件不能也不应该通过“原始”链接访问。

我的当前方法工作​​,除了urls,指向路径中没有文件名的子目录 - 在这种情况下,子域重新包含在重写的URL的路径中:

https://foo.bar.com/this/does/work.html

提供位于的文件

https://bar.com/foo/this/does/work.html

https://foo.bar.com/this/does/not/work

在浏览器中重写为

https://foo.bar.com/foo/this/does/not/work

由于显而易见的原因导致404。

但是,如果您直接在浏览器中输入包含问题的网址

https://foo.bar.com/this/does/not/work

它按预期工作。

我知道我在松散意义上使用“明显”这个词。这是一个快速演示:

https://egal.todoservice.app/

你应该看到那里,页面上的链接到https://egal.todoservice.app/sub/dir/

带你去https://egal.todoservice.app/egal/sub/dir/

但是尝试直接在地址栏中输入该链接并且它可以正常工作 - 它至少对我有用,在Chrome,Edge和Firefox中进行测试。

更新:奇怪的是,在stackoverflow上使用subdir链接按预期工作...

这是我的web.config中负责子域重写的部分

<rule name="Rewrite Subdomain To Directory" stopProcessing="false">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
    </conditions>
    <action type="Rewrite" url="{C:1}/{R:0}" />
</rule>

这是我的完整web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <staticContent>
             <mimeMap fileExtension="." mimeType="text/plain" />
             <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
         </staticContent>        
        <rewrite>
            <rules>
                <rule name="Rewrite SMZC URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.sadmenszombieclub.com"  />
                    </conditions>
                    <action type="Rewrite" url="smzc/{R:0}" />
                </rule>
                <rule name="Rewrite imagiro URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.imagiro.net"  />
                    </conditions>
                    <action type="Rewrite" url="imagiro/{R:0}" />
                </rule>

                <rule name="Rewrite Subdomain To Directory" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}/{R:0}" />
                </rule>

                <rule name="Rewrite root path to service Directory" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="service/{R:0}" />
                </rule>                
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

整个站点作为Azure Web App托管。

iis url-rewriting web-config azure-web-sites
1个回答
1
投票

复制评论。

您所做的不仅仅是简单的重写,而是反向代理。因此,需要进行一些改变,

  1. 在您的操作中,您还应该将完整的域名附加到<action type="Rewrite" url="{C:1}/{R:0}" />,而不是重写实际路径(url)。通过这种方式,IIS知道您要设置反向代理。
  2. 通过关注https://tomssl.com/2015/06/15/create-your-own-free-reverse-proxy-with-azure-web-apps/等博客文章,在Azure App Service中启用ARR代理模式
© www.soinside.com 2019 - 2024. All rights reserved.