Azure 应用程序网关 #
什么是应用程序网关? #
Azure 应用程序网关是第 7 层(应用层)负载均衡器,提供 Web 应用流量管理和安全功能。
text
┌─────────────────────────────────────────────────────────────┐
│ 应用程序网关概览 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 核心功能 │
│ ├── 第 7 层负载均衡 │
│ ├── SSL 终止 │
│ ├── 基于 URL 的路由 │
│ ├── Cookie 会话亲和性 │
│ ├── Web 应用防火墙 (WAF) │
│ └── 自动扩展 │
│ │
│ 适用场景 │
│ ├── Web 应用负载均衡 │
│ ├── 微服务路由 │
│ ├── API 网关 │
│ ├── SSL 卸载 │
│ └── Web 安全防护 │
│ │
│ 与负载均衡器对比 │
│ ├── 负载均衡器: 第 4 层,传输层 │
│ └── 应用网关: 第 7 层,应用层 │
│ │
└─────────────────────────────────────────────────────────────┘
SKU 类型 #
v2 SKU 对比 #
text
┌─────────────────────────────────────────────────────────────┐
│ SKU 类型 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 标准 v2 │
│ ├── 负载均衡 │
│ ├── SSL 终止 │
│ ├── URL 路由 │
│ ├── 自动扩展 │
│ └── 适合: 通用 Web 应用 │
│ │
│ WAF v2 │
│ ├── 包含标准 v2 所有功能 │
│ ├── Web 应用防火墙 │
│ ├── OWASP 规则 │
│ └── 适合: 需要安全防护的应用 │
│ │
│ 层级 │
│ ├── 标准: 基础功能 │
│ └── WAF: 安全增强 │
│ │
└─────────────────────────────────────────────────────────────┘
创建应用程序网关 #
使用 Azure CLI #
bash
# 创建资源组
az group create --name myResourceGroup --location eastus
# 创建虚拟网络
az network vnet create \
--resource-group myResourceGroup \
--name myVNet \
--address-prefix 10.0.0.0/16 \
--subnet-name myAGSubnet \
--subnet-prefix 10.0.1.0/24
# 创建后端子网
az network vnet subnet create \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myBackendSubnet \
--address-prefix 10.0.2.0/24
# 创建公共 IP
az network public-ip create \
--resource-group myResourceGroup \
--name myAGPublicIP \
--sku Standard
# 创建应用程序网关
az network application-gateway create \
--name myAppGateway \
--location eastus \
--resource-group myResourceGroup \
--vnet-name myVNet \
--subnet myAGSubnet \
--capacity 2 \
--sku Standard_v2 \
--http-settings-cookie-based-affinity Enabled \
--public-ip-address myAGPublicIP \
--servers 10.0.2.4 10.0.2.5
核心组件 #
组件架构 #
text
┌─────────────────────────────────────────────────────────────┐
│ 应用网关组件 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 前端 IP 配置 │
│ ├── 公共 IP │
│ └── 私有 IP │
│ │
│ 监听器 │
│ ├── 端口 │
│ ├── 协议 (HTTP/HTTPS) │
│ ├── SSL 证书 │
│ └── 主机名 │
│ │
│ 路由规则 │
│ ├── 路径规则 │
│ ├── 重定向 │
│ └── 重写 │
│ │
│ 后端池 │
│ ├── IP 地址 │
│ ├── FQDN │
│ └── App Service │
│ │
│ HTTP 设置 │
│ ├── 后端端口 │
│ ├── 协议 │
│ ├── 健康探测 │
│ └── Cookie 亲和性 │
│ │
└─────────────────────────────────────────────────────────────┘
流量流程 #
text
┌─────────────────────────────────────────────────────────────┐
│ 流量流程 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 客户端请求 │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 前端 IP │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 监听器 │ ← SSL 终止 │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 路由规则 │ ← URL 路由 │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 后端池 │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
URL 路由 #
基于路径的路由 #
bash
# 创建 URL 路径映射
az network application-gateway url-path-map create \
--gateway-name myAppGateway \
--resource-group myResourceGroup \
--name myPathMap \
--path-rules /images/* imagesBackend /api/* apiBackend \
--default-address-pool defaultBackend \
--default-http-settings myHttpSettings
# 添加路径规则
az network application-gateway url-path-map rule create \
--gateway-name myAppGateway \
--resource-group myResourceGroup \
--path-map-name myPathMap \
--name videosRule \
--paths /videos/* \
--address-pool videosBackend \
--http-settings myHttpSettings
路由示例 #
text
┌─────────────────────────────────────────────────────────────┐
│ URL 路由示例 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 路径规则: │
│ │
│ /images/* → 图片服务器池 │
│ /api/* → API 服务器池 │
│ /videos/* → 视频服务器池 │
│ /* → 默认 Web 服务器池 │
│ │
│ 示例请求: │
│ GET /images/photo.jpg → 图片服务器池 │
│ GET /api/users → API 服务器池 │
│ GET /about → 默认 Web 服务器池 │
│ │
└─────────────────────────────────────────────────────────────┘
SSL 配置 #
SSL 终止 #
bash
# 上传 SSL 证书
az network application-gateway ssl-cert create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name mySSLCert \
--cert-file ./certificate.pfx \
--cert-password <password>
# 创建 HTTPS 监听器
az network application-gateway http-listener create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name myHTTPSListener \
--frontend-port myHTTPSPort \
--ssl-cert mySSLCert
SSL 策略 #
bash
# 配置 SSL 策略
az network application-gateway ssl-policy set \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--policy-type Predefined \
--policy-name AppGwSslPolicy20170401S
Web 应用防火墙 (WAF) #
WAF 概述 #
text
┌─────────────────────────────────────────────────────────────┐
│ WAF 功能 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 防护类型 │
│ ├── SQL 注入 │
│ ├── 跨站脚本 (XSS) │
│ ├── 命令注入 │
│ ├── 文件包含 │
│ ├── 协议攻击 │
│ └── 更多 OWASP Top 10 │
│ │
│ 规则集 │
│ ├── OWASP 3.2 (推荐) │
│ ├── OWASP 3.1 │
│ ├── OWASP 3.0 │
│ └── OWASP 2.2.9 │
│ │
│ 模式 │
│ ├── 检测模式: 仅记录 │
│ └── 防护模式: 阻止攻击 │
│ │
└─────────────────────────────────────────────────────────────┘
配置 WAF #
bash
# 创建 WAF 策略
az network application-gateway waf-policy create \
--resource-group myResourceGroup \
--name myWAFPolicy
# 配置 WAF 规则
az network application-gateway waf-policy policy-setting update \
--resource-group myResourceGroup \
--policy-name myWAFPolicy \
--mode Prevention \
--state Enabled
# 关联 WAF 策略到应用网关
az network application-gateway update \
--resource-group myResourceGroup \
--name myAppGateway \
--set firewalls.policy.id=/subscriptions/.../myWAFPolicy
自定义 WAF 规则 #
bash
# 添加自定义规则
az network application-gateway waf-policy custom-rule create \
--resource-group myResourceGroup \
--policy-name myWAFPolicy \
--name blockIPs \
--priority 100 \
--rule-type MatchRule \
--action Block
# 添加匹配条件
az network application-gateway waf-policy custom-rule match-condition add \
--resource-group myResourceGroup \
--policy-name myWAFPolicy \
--rule-name blockIPs \
--match-variable RemoteAddr \
--operator IPMatch \
--values "192.168.1.1" "192.168.1.2"
健康探测 #
配置健康探测 #
bash
# 创建健康探测
az network application-gateway probe create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name myProbe \
--protocol http \
--path /health \
--interval 30 \
--timeout 30 \
--threshold 3
# 关联探测到 HTTP 设置
az network application-gateway http-settings update \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name myHttpSettings \
--probe myProbe
重定向和重写 #
HTTP 到 HTTPS 重定向 #
bash
# 创建重定向配置
az network application-gateway redirect-config create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name httpToHttps \
--type Permanent \
--target-listener myHTTPSListener
# 关联到 HTTP 监听器
az network application-gateway http-listener update \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name myHTTPListener \
--redirect-config httpToHttps
重写规则 #
bash
# 创建重写规则集
az network application-gateway rewrite-rule set create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--name myRewriteSet
# 添加重写规则
az network application-gateway rewrite-rule create \
--resource-group myResourceGroup \
--gateway-name myAppGateway \
--rule-set-name myRewriteSet \
--name addHeader \
--sequence 100 \
--response-headers "X-Custom-Header=MyValue"
最佳实践 #
设计建议 #
text
┌─────────────────────────────────────────────────────────────┐
│ 设计最佳实践 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 使用 WAF v2 SKU │
│ └── 获得安全防护功能 │
│ │
│ 2. 启用自动扩展 │
│ └── 根据流量自动调整容量 │
│ │
│ 3. 配置健康探测 │
│ └── 确保流量只发往健康后端 │
│ │
│ 4. 使用 SSL 终止 │
│ └── 卸载 SSL 处理 │
│ │
│ 5. 配置会话亲和性 │
│ └── 保持用户会话一致性 │
│ │
│ 6. 使用私有链接 │
│ └── 保护后端连接 │
│ │
└─────────────────────────────────────────────────────────────┘
安全建议 #
text
┌─────────────────────────────────────────────────────────────┐
│ 安全最佳实践 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 启用 WAF 防护模式 │
│ └── 阻止检测到的攻击 │
│ │
│ 2. 使用最新 OWASP 规则集 │
│ └── 获得最新防护 │
│ │
│ 3. 配置 SSL 策略 │
│ └── 使用强加密套件 │
│ │
│ 4. 启用端到端 SSL │
│ └── 加密后端连接 │
│ │
│ 5. 定期审查 WAF 日志 │
│ └── 监控攻击情况 │
│ │
└─────────────────────────────────────────────────────────────┘
下一步 #
现在你已经掌握了应用程序网关的使用,接下来学习 安全服务 了解 Azure 安全解决方案!
最后更新:2026-03-29