Why does HttpPlatformHandler/ANCMv2 for IIS only assign a port dynamically and how to use ARR/URL Rewrite given this

Mark Zhukovsky 46 Reputation points
2023-08-14T14:52:43.3866667+00:00

This question is 2 parts - would really appreciate any insight.

  1. What are the architectural reasons behind the port assignment via HttpPlatformHandler to another process, let's say a Python web app, only being dynamic? i.e. it is not possible to specify, explicitly, a port that should be used to run the process, and instead it is only determined once a request comes through and the module finds an open port dynamically to pass the socket connection onto - https://learn.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference - so only the env variable HTTP_PLATFORM_PORT (or ASPNETCORE_PORT if using ANCMv2) will indicate this once the process is started.
  2. Given this fact, please confirm then that it is not possible to define appropriate rules using ARR/URL Rewrite modules to proxy some requests directly to the web server being run by the owned process, since those variables are not accessible - it may seem strange to even ask this but due to some limitations of a 3rd party web app framework I am looking for a workaround of sorts.
Internet Information Services
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lex Li (Microsoft) 6,032 Reputation points Microsoft Employee
    2023-08-14T16:03:14.97+00:00

    Dynamic port selection makes perfect sense, as HttpPlatformHandler has a feature to restart your web app and there isn't a guarantee that the same port is always available.

    You don't need extra ARR/URL rewrite rules with HttpPlatformHandler (unless you try to perform generic rewriting like HTTP to HTTPS), and you don't need to access HTTP_PLATFORM_PORT yourself.

    Update: Mar 2025

    In case you want to fully take control of the port number between IIS and your application server, instead of depending on HttpPlatformHandler's random choice, HttpPlatformHandler v2 (https://github.com/lextudio/httpplatformhandlerv2) allows you to make your own decision, with sample settings as below,

            <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\node_modules\next\dist\bin\next start">
                <environmentVariables>
                    <environmentVariable name="PORT" value="4000" />
                    <environmentVariable name="NODE_ENV" value="production" />
                    <environmentVariable name="DEBUG" value="socket*" />
                    <environmentVariable name="HTTP_PLATFORM_PORT" value="4000" />
                </environmentVariables>
            </httpPlatform>
    

    You can see that by setting an explicit port of 4000, the backend Node.js server will monitor port 4000 instead of a random port and you can access the same default page using http://localhost:4000. IIS/HttpPlatformHandler v2 in this case will forward the incoming traffic to port 4000 and enable reverse proxy rules.

    This is still different from runnning your Node.js server on port 4000 and use IIS/ARR as reverse proxy, because HttpPlatformHandler still manages the Node.js process for you so things like implicit reverse proxy rules, application pool recycle support, app_offline.htm support, etc. continue to work as you might expected. With ARR you lose all of them and have to manage Node.js processes fully on your own.

    Note that this behavior is only possible with HttpPlatformHandler v2 (as it inherites this feature from ASP.NET Core module). The original v1.2 from Microsoft doesn't support it.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 74,936 Reputation points
    2023-08-14T16:04:45.2233333+00:00

    the ports are assigned dynamically because every hosted app needs a unique port. if multiple processes are configured, than each needs its own port.

    your goal is not clear. The HttpPlatformHandler is acting as a reverse proxy to the hosted application site. ARR/Url only work on requests handled by IIS. you can use url rewrite to map IIS requests to the handler. as IIS will not see actual requests to hosted app's port, not sure what you are trying to do.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.