反向代理配置命令 appcmd.exe (server farm)
https://docs.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/define-and-configure-an-application-request-routing-server-farm
程序路径:C:\Windows\system32\inetsrv\appcmd.exe
执行命令的方法1: C:\Windows\system32\inetsrv\appcmd.exe list site
执行命令的方法2: C:\Windows\system32\inetsrv\appcmd list site
原理:通过命令找到xml 节点 进行控制
1 创建farm
appcmd.exe set config -section:webFarms /+"[name='myServerFarm']" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328232713168-1213814538.png)
2. 创建应用代理服务器
appcmd.exe set config -section:webFarms /+"[name='myServerFarm'].[address='192.168.199.101']" /commit:apphost
appcmd.exe set config -section:webFarms /+"[name='myServerFarm'].[address='192.168.199.102']" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233014452-1510809111.png)
更代理的应用程序端口(一般是多版本发布的时候端口是随机的,所以端口要变)
appcmd.exe set config -section:webFarms /"[name='myServerFarm'].[address='192.168.199.101'].applicationRequestRouting.httpPort:40001" /commit:apphost
appcmd.exe set config -section:webFarms /"[name='myServerFarm'].[address='192.168.199.102'].applicationRequestRouting.httpPort:40001" /commit:apphost
应用服务器上线:
appcmd.exe set config -section:webFarms /"[name='myServerFarm'].[address='192.168.199.101'].enabled:True" /commit:apphost
应用服务器离线:
appcmd.exe set config -section:webFarms /"[name='myServerFarm'].[address='192.168.199.101'].enabled:False" /commit:apphost
3.创建url 重写规则
3.1 创建url 重写规则名称
appcmd.exe set config -section:system.webServer/rewrite/globalRules /+"[name='ARR_myServerFarm_loadbalance', patternSyntax='Wildcard']" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233048431-2108784853.png)
3.2 创建url重写规则的 匹配模式 为 【*】 通配
appcmd.exe set config -section:system.webServer/rewrite/globalRules /[name='ARR_myServerFarm_loadbalance',patternSyntax='Wildcard'].match.url:"*" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233121614-1496758826.png)
3.3 创建url 重写规则的路由模式: 【路由到场】
appcmd.exe set config -section:system.webServer/rewrite/globalRules /[name='ARR_myServerFarm_loadbalance',patternSyntax='Wildcard'].action.type:"Rewrite" /[name='ARR_myServerFarm_loadbalance',patternSyntax='Wildcard'].action.url:"http://myServerFarm/{R:0}" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233205999-1297788240.png)
3.4 创建反向代理端口
appcmd.exe set config -section:system.webServer/rewrite/globalRules /+"[name='ARR_myServerFarm_loadbalance
'].conditions.[input='{SERVER_PORT}',pattern='7777']" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233236315-1683874514.png)
3.5 启用url 重写(启用url规则)
appcmd.exe set config -section:system.webServer/rewrite/globalRules /[name='ARR_myServerFarm_loadbalance
'].stopProcessing:"False" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233313747-48036639.png)
3.5 禁用url 重写(禁用url规则)
appcmd.exe set config -section:system.webServer/rewrite/globalRules /[name='ARR_myServerFarm_loadbalance
'].stopProcessing:"True" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200328233343677-1294544070.png)
=================================================其他配置======================================
1.禁用cache 缓存
appcmd.exe set config -section:webFarms /[name='myServerFarm
'].applicationRequestRouting.protocol.cache.enabled:"False" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200329000655832-396421801.png)
2.设置反向代理超时时间(相当于普通web应用的连接超时时间:格式为 时:分:秒,下图是设置为5分钟,即 300秒
)
appcmd.exe set config -section:webFarms /[name='myServerFarm
'].applicationRequestRouting.protocol.timeout:"00:05:00" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200329000752263-1744219533.png)
2.健康检查URL配置
appcmd.exe set config -section:webFarms /[name='myServerFarm
'].applicationRequestRouting.healthCheck.url:"http://192.168.1.2" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200329000826870-1426463424.png)
3.健康检查 时间间隔 ,格式为 时:分:秒 ,下图是设置为5秒
appcmd.exe set config -section:webFarms /[name='myServerFarm
'].applicationRequestRouting.healthCheck.interval:"00:00:05" /commit:apphost
![](https://img2020.cnblogs.com/blog/550652/202003/550652-20200329000838853-1142773880.png)
==================其他命令=========================
列出所有的webfarm节点
APPCMD list config /section:webfarms
![](https://img2020.cnblogs.com/blog/550652/202012/550652-20201215141557805-1565414949.png)