You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure App Service中基于子域名实现端口间请求路由?

在Azure App Service中实现多域名路由到内部多站点的方案

核心思路

Azure App Service对外仅暴露80/443端口,无法直接让内部多站点的端口对外访问。必须通过内部反向代理,根据请求域名将流量转发到对应端口的内部站点,以此实现多域名独立站点的效果,同时避免虚拟目录对SEO的影响。

方案1:Windows App Service(基于IIS反向代理)

Windows环境下的App Service自带IIS,无需额外安装,仅需配置URL重写规则即可实现反向代理:

  1. 将多个子站点部署到App Service的不同目录(如site\wwwroot\miracles、site\wwwroot\scientist),并确保每个子站点监听内部端口(推荐8081、8082等,避开80/443/8080这类对外/默认端口)。
  2. 在站点根目录创建web.config文件,添加域名路由规则:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- 路由到miracles.corey.com -->
        <rule name="Route to Miracles" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^miracles\.corey\.com$" />
          </conditions>
          <action type="Rewrite" url="http://localhost:8081/{R:1}" />
        </rule>
        <!-- 路由到scientist.corey.com -->
        <rule name="Route to Scientist" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^scientist\.corey\.com$" />
          </conditions>
          <action type="Rewrite" url="http://localhost:8082/{R:1}" />
        </rule>
        <!-- 默认路由(可选,匹配未指定的域名) -->
        <rule name="Default Route" stopProcessing="true">
          <match url="(.*)" />
          <action type="Rewrite" url="http://localhost:8080/{R:1}" />
        </rule>
      </rules>
    </rewrite>
    <!-- 启用IIS反向代理支持 -->
    <proxy enabled="true" />
  </system.webServer>
</configuration>

方案2:Linux App Service(基于Nginx反向代理)

Linux环境下的App Service默认使用Nginx作为前端服务器,只需修改Nginx配置即可实现域名路由:

  1. 在App Service的/home/site目录下创建nginx文件夹,新建配置文件default(路径:/home/site/nginx/default),添加路由规则:
server {
    listen 8080;
    server_name _;
    root /home/site/wwwroot;

    # 处理miracles.corey.com的请求
    location / {
        if ($host = miracles.corey.com) {
            proxy_pass http://localhost:8081;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    # 处理scientist.corey.com的请求
    location / {
        if ($host = scientist.corey.com) {
            proxy_pass http://localhost:8082;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    # 默认站点路由
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
  1. 通过App Service的启动命令加载自定义Nginx配置并启动所有站点:
cp /home/site/nginx/default /etc/nginx/sites-available/default && service nginx reload && <你的主站点启动命令> && <子站点1启动命令> && <子站点2启动命令>
  1. 若需管理多个子站点进程,推荐使用supervisord编写配置文件,确保所有站点进程稳定运行。

关键注意事项

  • 内部端口选择:务必避开80、443、8080,选择8081及以上未被占用的端口。
  • 域名配置:将所有子域名(如miracles.corey.com)添加到App Service的自定义域名列表,并完成DNS解析。
  • SSL证书:为所有子域名配置SSL证书(可使用App Service免费证书或自定义证书),确保HTTPS访问正常。
  • 进程监控:Linux环境下需确保多站点进程同时运行,避免单个进程退出导致对应站点不可用。

内容的提问来源于stack exchange,提问作者Corey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:05:01