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

如何使用PowerShell创建带反向代理功能的IIS站点指向本地Windows服务

PowerShell的IIS WebAdministration模块完全支持操作URL Rewrite 2的所有配置,因为URL Rewrite的规则本质是存储在IIS的XML配置文件中,该模块可以直接读写对应配置节点,无需额外适配。

前置依赖检查
  • 服务器需提前安装必备组件:IIS服务、URL Rewrite 2、Application Request Routing(ARR,IIS反向代理必备组件)、IIS PowerShell管理模块
  • ARR安装完成后需要手动启用代理功能,否则反向代理规则不会生效
完整操作脚本
# 导入IIS管理模块
Import-Module WebAdministration

# 启用ARR全局代理功能
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.webServer/proxy" -name "enabled" -value "True"

# 自定义配置项,可按需修改
$siteName = "DotnetGenericHostProxy"
$sitePhysicalPath = "C:\inetpub\$siteName"
$bindingProtocol = "https"
$bindingPort = 443
$bindingHostHeader = "somesite.com"
$backendServiceUrl = "http://localhost:9001/"

# 创建站点物理目录(反向代理站点不需要实际站点文件,空目录即可)
New-Item -Path $sitePhysicalPath -ItemType Directory -Force

# 创建IIS站点
New-WebSite -Name $siteName -PhysicalPath $sitePhysicalPath -Port $bindingPort -HostHeader $bindingHostHeader -Protocol $bindingProtocol -Ssl

# 为站点添加反向代理规则
Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST/$siteName" -filter "system.webServer/rewrite/rules" -name "." -value @{
    name = 'ReverseProxy_To_Dotnet_WindowsService'
    stopProcessing = 'True'
    match = @{
        url = '(.*)'
        ignoreCase = 'True'
    }
    action = @{
        type = 'Rewrite'
        url = "$backendServiceUrl{R:1}"
        appendQueryString = 'True'
    }
}

注意:如果对应IIS站点已经提前创建完成,可跳过站点创建步骤,直接修改$siteName为已有站点名后执行规则添加命令即可。

配置文档查询路径
  • IIS PowerShell管理模块的命令参考可查询微软官方文档的Web Administration模块分区
  • URL Rewrite规则的所有可配置属性参考,可查询微软官方文档的URL Rewrite Module Configuration Reference分区

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 05:45:03