通过


HttpPlatformHandler 配置参考

由 IIS 团队

本文概述了 HttpPlatformHandler,并介绍了模块的配置。

功能概述

HttpPlatformHandler 是 IIS 模块,适用于 IIS 8+,执行以下两项操作:

  1. http 侦听器的进程管理 - 这可能是任何可以侦听 http 请求端口的进程。 例如 - Tomcat、Jetty、Node.exe、Ruby 等;
  2. 向它管理的进程发出代理请求。

HttpPlatformHandler 配置

HttpPlatformHandler 通过站点或应用程序的 web.config 文件进行配置,并在 system.webServer 的 httpPlatform 配置节中有其自己独立的配置。

配置属性

属性 说明
processPath 必需属性。 将启动侦听 HTTP 请求的进程的可执行文件或脚本的路径。 从 v1.2 版本开始支持相对路径,如果路径以“.”开头,则路径被视为相对于站点根路径。 没有默认值
arguments 可选字符串值。 processPath 设置中指定的可执行文件或脚本的参数。 没有默认值。
startupTimeLimit 可选的整数属性。 HttpPlatformHandler 等待可执行文件/脚本启动一个侦听端口的进程的持续时间,单位为秒。 如果超出此时间限制,HttpPlatformHandler 将终止进程,并尝试再次启动 startupRetryCount 次。 默认值为 10
startupRetryCount 可选的整数属性。 HttpPlatformHandler 尝试启动 processPath 中指定的进程的次数。 有关更多详细信息,请参阅 startupTimeLimit 。 默认值为 10
rapidFailsPerMinute 可选的整数属性。 指定允许 processPath 中指定的进程每分钟崩溃的次数。 如果超出此限制, HttpPlatformHandler 将在分钟剩余时间内停止启动进程。 managedPipelineMode 属性可以是以下可能值之一。 默认值为 10
requestTimeout 可选的时间跨度属性。 指定 HttpPlatformHandler 等待进程侦听 %HTTP_PLATFORM_PORT%的响应的持续时间。 默认值为“00:02:00”。
stdoutLogEnabled 可选的布尔属性。 如果为 true,则 processPath 设置中指定的进程的 stdoutstderr 将重定向到 stdoutLogFile 中指定的文件。默认值为 false.
stdoutLogFile 可选的字符串属性。 指定将记录 processPath 中指定的进程的 stdoutstderr 的相对 OR 绝对文件路径。 相对路径是相对于网站根目录的。 从 v1.2 开始,任何以“.”开头的路径都将相对于站点根目录,所有其他路径都将被视为绝对路径。 默认值为 httpplatform-stdout.
processesPerApplication 可选的整数属性。 指定可为每个应用程序启动的多个进程实例数量,这些进程是通过processPath设置指定的。 最大值为 100。 默认值为 1
forwardWindowsAuthToken 对或错。 v1.2 的新增功能。 如果此设置设置为 true,则令牌将转发到子进程,侦听 %HTTP_PLATFORM_PORT% 作为每个请求的标头“X-IIS-WindowsAuthToken”。 此过程负责在每个请求中对此令牌调用 CloseHandle。 默认值为 false

子元素

元素 说明
environmentVariables processPath 设置中指定的进程配置 environmentVariables 集合。
recycleOnFileChange 配置文件集合,当对指定列表中的 文件 进行更改时回收工作进程。 元素语法,例如 <文件路径=“。\touch.txt”/> 或 <文件路径=“c:\file.txt”/>

HttpPlatformHandler 配置示例

下面是运行多个具有不同进程的应用程序的配置示例。

Tomcat

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="c:\dev\javasites\bin\apache-tomcat-8.0.15\bin\startup.bat" 
                  arguments="" 
                  stdoutLogEnabled="true" 
                  stdoutLogFile="\\?c:\dev\javasites\log.txt">
      <environmentVariables>
        <environmentVariable name="JRE_HOME" value="%programfiles%\java\jdk1.8.0_25" />
        <environmentVariable name="CATALINA_OPTS" value="-Dport.http=%HTTP_PLATFORM_PORT% -Dsomeotherconfig=value"/>
        <environmentVariable name="CATALINA_HOME" value="c:\dev\javasites\bin\apache-tomcat-8.0.15" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>

码头

<?xml version="1.0"?>
<configuration>
    <system.webServer>
      <handlers>
        <add name="httpplatformhandler" path="*" verb="*" modules ="httpPlatformHandler" resourceType="Unspecified"/>
      </handlers>
      <httpPlatform processPath="c:\Program Files\Java\jdk1.8.0_25\bin\java.exe" 
                    arguments="-Djava.net.preferIPv4Stack=true -Djetty.port=%HTTP_PLATFORM_PORT% -Djetty.base=&quot;c:\dev\javasites\bin\jetty-distribution-9.2.6.v20141205&quot; -jar &quot;c:\dev\javasites\bin\jetty-distribution-9.2.6.v20141205\start.jar&quot;" 
                    startupTimeLimit="20" 
                    startupRetryCount="20" 
                    stdoutLogEnabled="true"></httpPlatform>
    </system.webServer>
</configuration>

示例代码

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {
        
        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("JavaSites");
            
            ConfigurationSection httpPlatformSection = config.GetSection("system.webServer/httpPlatform");
            httpPlatformSection["processPath"] = @"c:\dev\javasites\bin\apache-tomcat-8.0.15\bin\startup.bat";
            httpPlatformSection["arguments"] = @"";
            httpPlatformSection["stdoutLogFile"] = @"=""\\?c:\dev\javasites\log.txt";
            
            ConfigurationElementCollection environmentVariablesCollection = httpPlatformSection.GetCollection("environmentVariables");
            
            ConfigurationElement environmentVariableElement = environmentVariablesCollection.CreateElement("environmentVariable");
            environmentVariableElement["name"] = @"JRE_HOME";
            environmentVariableElement["value"] = @"=""%programfiles%\java\jdk1.8.0_25";
            environmentVariablesCollection.Add(environmentVariableElement);
            
            ConfigurationElement environmentVariableElement1 = environmentVariablesCollection.CreateElement("environmentVariable");
            environmentVariableElement1["name"] = @"CATALINA_OPTS";
            environmentVariableElement1["value"] = @"=""-Dport.http=%HTTP_PLATFORM_PORT% -Dsomeotherconfig=value";
            environmentVariablesCollection.Add(environmentVariableElement1);
            
            ConfigurationElement environmentVariableElement2 = environmentVariablesCollection.CreateElement("environmentVariable");
            environmentVariableElement2["name"] = @"CATALINA_HOME";
            environmentVariableElement2["value"] = @"c:\dev\javasites\bin\apache-tomcat-8.0.15";
            environmentVariablesCollection.Add(environmentVariableElement2);
            
            serverManager.CommitChanges();
        }
    }
}

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/JavaSites";

var httpPlatformSection = adminManager.GetAdminSection("system.webServer/httpPlatform", "MACHINE/WEBROOT/APPHOST/JavaSites");
httpPlatformSection.Properties.Item("processPath").Value = "c:\\dev\\javasites\\bin\\apache-tomcat-8.0.15\\bin\\startup.bat";
httpPlatformSection.Properties.Item("arguments").Value = "";
httpPlatformSection.Properties.Item("stdoutLogFile").Value = "=\"\\\\?c:\\dev\\javasites\\log.txt";

var environmentVariablesCollection = httpPlatformSection.ChildElements.Item("environmentVariables").Collection;

var environmentVariableElement = environmentVariablesCollection.CreateNewElement("environmentVariable");
environmentVariableElement.Properties.Item("name").Value = "JRE_HOME";
environmentVariableElement.Properties.Item("value").Value = "=\"%programfiles%\\java\\jdk1.8.0_25";
environmentVariablesCollection.AddElement(environmentVariableElement);

var environmentVariableElement1 = environmentVariablesCollection.CreateNewElement("environmentVariable");
environmentVariableElement1.Properties.Item("name").Value = "CATALINA_OPTS";
environmentVariableElement1.Properties.Item("value").Value = "=\"-Dport.http=%HTTP_PLATFORM_PORT% -Dsomeotherconfig=value";
environmentVariablesCollection.AddElement(environmentVariableElement1);

var environmentVariableElement2 = environmentVariablesCollection.CreateNewElement("environmentVariable");
environmentVariableElement2.Properties.Item("name").Value = "CATALINA_HOME";
environmentVariableElement2.Properties.Item("value").Value = "c:\\dev\\javasites\\bin\\apache-tomcat-8.0.15";
environmentVariablesCollection.AddElement(environmentVariableElement2);

adminManager.CommitChanges();

命令行 (AppCmd)

appcmd.exe set config "JavaSites" -section:system.webServer/httpPlatform /processPath:"c:\dev\javasites\bin\apache-tomcat-8.0.15\bin\startup.bat" /arguments:"" /stdoutLogFile:"="""\\?c:\dev\javasites\log.txt"  

appcmd.exe set config "JavaSites" -section:system.webServer/httpPlatform /+"environmentVariables.[name='JRE_HOME',value='="""%programfiles%\java\jdk1.8.0_25']" 

appcmd.exe set config "JavaSites" -section:system.webServer/httpPlatform /+"environmentVariables.[name='CATALINA_OPTS',value='="""-Dport.http=%HTTP_PLATFORM_PORT% -Dsomeotherconfig=value']" 

appcmd.exe set config "JavaSites" -section:system.webServer/httpPlatform /+"environmentVariables.[name='CATALINA_HOME',value='c:\dev\javasites\bin\apache-tomcat-8.0.15']"

PowerShell

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform" -name "processPath" -value "c:\dev\javasites\bin\apache-tomcat-8.0.15\bin\startup.bat"
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform" -name "arguments" -value ""
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform" -name "stdoutLogFile" -value "="""\\?c:\dev\javasites\log.txt"

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform/environmentVariables" -name "." -value @{name='JRE_HOME';value='="""%programfiles%\java\jdk1.8.0_25'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform/environmentVariables" -name "." -value @{name='CATALINA_OPTS';value='="""-Dport.http=%HTTP_PLATFORM_PORT% -Dsomeotherconfig=value'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/JavaSites'  -filter "system.webServer/httpPlatform/environmentVariables" -name "." -value @{name='CATALINA_HOME';value='c:\dev\javasites\bin\apache-tomcat-8.0.15'}