配置IIS Rewrite与HttpPlatformHandler部署Quarkus API及方案选型咨询
问题解答:IIS部署Quarkus API的配置与方案选择
问题1:是否可以同时配置rewrite和httpPlatform?若可以,如何实现?
可以同时配置,但原配置的核心问题是主站点rewrite规则直接绕过了子应用,导致HttpPlatformHandler未被触发。正确思路是让/api开头的请求先进入子应用,由子应用的HttpPlatformHandler启动Quarkus进程,再完成请求转发。具体配置调整如下:
主站点web.config(MainApp/web.config)
保留<location path="." inheritInChildApplications="false">避免子应用继承主站点配置,修改rewrite规则将请求导向子应用路径:
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <httpErrors errorMode="Detailed" /> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath=".\MainWeb.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /> <rewrite> <rules> <rule name="QuarkusAPI" stopProcessing="true"> <match url="^api/(.*)" /> <!-- 转发到子应用路径,触发HttpPlatformHandler启动进程 --> <action type="Rewrite" url="/api/{R:1}" logRewrittenUrl="true" /> </rule> </rules> </rewrite> </system.webServer> </location> </configuration>
子应用web.config(QuarkusAPI/web.config)
完善httpPlatform配置,通过环境变量指定Quarkus监听端口(或使用Handler动态分配端口),确保请求能正确转发到Quarkus进程:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="QuarkusAPIHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" /> </handlers> <httpPlatform processPath="D:\QuarkusAPI\quarkus-rest-api.exe" stdoutLogEnabled="true" stdoutLogFile="D:\QuarkusAPI\logs\stdout.log" startupTimeLimit="600"> <!-- 指定Quarkus监听8080端口,也可替换为%HTTP_PLATFORM_PORT%让Handler动态分配 --> <environmentVariables> <environmentVariable name="QUARKUS_HTTP_PORT" value="8080" /> </environmentVariables> </httpPlatform> <httpErrors errorMode="Detailed" /> </system.webServer> </configuration>
关键注意事项
- 主站点rewrite规则将请求导向子应用路径,触发HttpPlatformHandler自动启动Quarkus进程;
- 子应用通过环境变量传递端口配置,确保Quarkus与Handler的转发端口一致;
- 需在IIS管理器中启用已安装的ARR(Application Request Routing)模块的反向代理功能。
问题2:Windows Service+IIS反向代理是否是更通用合适的方案?
是的,Windows Service+IIS反向代理是生产环境下更稳定、通用的方案,核心优势如下:
对比HttpPlatformHandler的优势
- 进程稳定性更高:HttpPlatformHandler依赖请求触发进程启动,长时间无请求可能被IIS回收;Windows Service可确保Quarkus进程持续运行,服务器重启后自动启动,彻底避免冷启动问题。
- 运维管理更便捷:Windows Service可通过系统服务管理器直观查看状态、设置自动启动,支持日志监控和故障自动重启;HttpPlatformHandler的进程管理隐藏在IIS背后,排查问题复杂度更高。
- 反向代理能力更成熟:IIS的ARR+Rewrite反向代理经过大量生产环境验证,支持负载均衡、压缩处理、复杂路由规则等高级功能,比HttpPlatformHandler的转发能力更完善。
具体实现步骤
- 将Quarkus注册为Windows Service
使用NSSM(Non-Sucking Service Manager)或sc命令完成注册,以NSSM为例:
# 安装服务,指定Quarkus可执行文件路径 nssm install QuarkusAPI D:\QuarkusAPI\quarkus-rest-api.exe # 设置服务为自动启动 nssm set QuarkusAPI Start SERVICE_AUTO_START # 启动服务 nssm start QuarkusAPI
- 主站点配置反向代理
修改主站点web.config的rewrite规则,将/api请求代理到localhost:8080,同时处理压缩编码避免乱码:
<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <httpErrors errorMode="Detailed" /> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath=".\MainWeb.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" /> <rewrite> <rules> <rule name="QuarkusAPI" stopProcessing="true"> <match url="^api/(.*)" /> <action type="Rewrite" url="http://localhost:8080/{R:1}" /> <!-- 处理压缩编码,避免代理后乱码 --> <serverVariables> <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" /> <set name="HTTP_ACCEPT_ENCODING" value="" /> </serverVariables> </rule> </rules> <outboundRules> <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding"> <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^$" /> <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" /> </rule> <preConditions> <preCondition name="NeedsRestoringAcceptEncoding"> <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" /> </preCondition> </preConditions> </outboundRules> </rewrite> </system.webServer> </location> </configuration>
- 启用IIS反向代理
在IIS管理器中,进入服务器节点 ->Application Request Routing Cache->服务器代理设置,勾选启用代理并保存配置。
内容的提问来源于stack exchange,提问作者tsushimaru
相关产品推荐
相关产品推荐

