【应用服务 App Service】 App Service Rewrite 实例 - 反向代理转发功能

简介: 【应用服务 App Service】 App Service Rewrite 实例 - 反向代理转发功能

问题描述

在使用Azure App Service(应用服务)时,有时候需要在不同的站点之间进行跳转,但是希望通过通过访问同一个域名的方式来实现反向代理。如果创建应用时候选择的是Window服务,这时候可以参考以下的方式配置IIS 的Proxy + Rewrie

如网站一为PHP站点,它的首页是:https://lbphptest.chinacloudsites.cn/, 网站二是Java站点,它的首页是:https://lbjavatest.chinacloudsites.cn/

在使用反向代理后,在站点一种配置Proxy代理后,请求就会发送到Java站点,但是URL显示的依旧为PHP站点地址。

设置步骤

 

一: 准备好两个App Service站点

二:在需要做方向代理的站点种添加applicationHost.xdt和web.config 文件,如以上的例子中使用的是PHP站点作为代理站点。

  • 添加ApplicationHost.xdt文件,开启Proxy enable属性为true。文件需要添加在D:\home\site下,与wwwroot目录同级

ApplicationHost.xdt的内容为:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" />
    </system.webServer>
</configuration>
  • 添加web.config文件,设置rewrite的规则。

web.config的内容为(高亮部分即为需要配置的跳转站点):

<configuration>  
<system.webServer>  
<rewrite>  
<rules>  
<rule name="Proxy" stopProcessing="true">  
<match url="^proxy/?(.*)" />  
<action type="Rewrite" url="http://lbjavatest.chinacloudsites.cn/{R:1}" />  
</rule>  
</rules>  
</rewrite>  
</system.webServer>  
</configuration>

注:完成以上步骤后,需要重启PHP站点。

 

参考资料

Web 应用如何实现反向代理转发功能:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-howto-realize-reverse-proxy-and-forward-function

 

URL Rewrite Module Configuration Reference:https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#redirect-action

Rewrite Rules Overview

A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful.

Rewrite rules consists of the following parts:

  • Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings.
  • Conditions – The optional conditions collection is used to specify additional logical operations to perform if a URL string matches the rule pattern. Within the conditions, you can check for certain values of HTTP headers or server variables, or verify if the requested URL corresponds to a file or directory on a physical file system.
  • Action – The action is used to specify what to do if the URL string matches the rule pattern and all the rule conditions are met.

 

附加一: App Service中启动jar包的web.config中加入rewrite规则

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
 
    <!-- BEGIN rule TAG FOR HTTPS REDIRECT -->
    <rewrite>
      <rules>
        <rule name="Force HTTPS" enabled="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="Initialization" ignoreCase="true" negate="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" ignoreCase="true" negate="true" />
                  <add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    <!-- END rule TAG FOR HTTPS REDIRECT -->
 
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <!-- https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference -->
    <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
         arguments="-Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -Dserver.port=%HTTP_PLATFORM_PORT% -jar "%HOME%\site\wwwroot\what.jar""
         startupTimeLimit="300"
         startupRetryCount="10"
         requestTimeout="00:05:00"
         >
    </httpPlatform>
  </system.webServer>
</configuration>


相关文章
|
4月前
|
小程序 视频直播 数据安全/隐私保护
如何在1v1视频直播交友APP中实现防录屏防截屏功能?
婚恋交友市场快速增长,1v1社交应用海外投放增86.49%,中东、东南亚及北美需求旺盛。用户偏好私密高效交流,国内“云相亲”兴起。开发需合规备案、实名认证,并防范诈骗。本文详解原生APP防录屏技术:Android通过MediaProjection检测,iOS监听UIScreen状态,结合动态水印、远程销毁等增强防护,平衡体验与安全。
|
4月前
|
JavaScript API 开发工具
如何在原生App中调用Uniapp的原生功能?
如何在原生App中调用Uniapp的原生功能?
754 139
|
5月前
|
移动开发 小程序 Android开发
基于 uni-app 开发的废品回收类多端应用功能与界面说明
本文将对一款基于 uni-app 开发的废品回收类多端应用,从多端支持范围、核心功能模块及部分界面展示进行客观说明,相关资源信息也将一并呈现。
195 0
|
4月前
|
Java 应用服务中间件 API
【App Service】部署War包到Azure云上遇404错误
Java应用部署至Azure App Service for Windows后报404,本地运行正常。经排查,日志提示类文件版本不兼容:应用由Java 17(class file version 61.0)编译,但环境仅支持到Java 11(55.0)。错误根源为Java版本不匹配。调整App Service的Java版本至17后问题解决,成功访问接口。
206 1
|
4月前
|
存储 自然语言处理 算法
参照Yalla、Hawa等主流APP核心功能,开发一款受欢迎的海外语聊需要从哪些方面入手
海外语聊APP开发需结合Yalla、Hawa等主流产品,聚焦多语言支持、实时音视频、社交互动与安全合规。兼顾技术架构、本地化运营与法律风险,避免劣质成品代码,平衡创新与成本,打造差异化出海产品。(239字)
|
4月前
|
存储 Linux 网络安全
【Azure App Service】Root CA on App Service
Azure App Service for Windows应用连接外部SSL服务时,需确保其证书由受信任的根CA颁发。多租户环境下无法修改根证书,但ASE(单租户)可加载自定义CA证书。若遇证书信任问题,可更换为公共CA证书或将应用部署于ASE并导入私有CA证书。通过Kudu的PowerShell(Windows)或SSH(Linux)可查看当前受信任的根证书列表。
121 13
|
5月前
|
存储 开发者 容器
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
本文介绍了ArkTS语言中的Class类、泛型、接口、模块化、自定义组件及状态管理等核心概念,并结合代码示例讲解了对象属性、构造方法、继承、静态成员、访问修饰符等内容,同时涵盖了路由管理、生命周期和Stage模型等应用开发关键知识点。
439 1
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
|
5月前
|
网络协议 Java Linux
【App Service】在Azure环境中如何查看App Service实例当前的网络连接情况呢?
在 Azure App Service(Windows 和 Linux)中部署应用时,分析网络连接状态是排查异常、验证端口监听及确认后端连接的关键。本文介绍如何在 Linux 环境中使用 `netstat` 命令查看特定端口(如 443、3306、6380)的连接情况,并解析输出结果。同时说明在 Windows App Service 中 `netstat` 被禁用的情况下,如何通过门户抓包等替代方法进行网络诊断。内容涵盖命令示例、操作步骤及附录说明,帮助开发者快速掌握云环境中的网络分析技巧。
151 11
|
4月前
|
缓存 移动开发 JavaScript
如何优化UniApp开发的App的启动速度?
如何优化UniApp开发的App的启动速度?
822 139
|
4月前
|
移动开发 JavaScript weex
UniApp开发的App在启动速度方面有哪些优势和劣势?
UniApp开发的App在启动速度方面有哪些优势和劣势?
423 137