Server Options

Configure runtime behaviors (headers, compression, runspaces) with Set-KrServerOptions.

Prerequisites: Endpoints basics; review Logging to observe effects.

Full source

File: pwsh/tutorial/examples/13.2-Server-Options.ps1

<#
    13.2 Server Options Example
    Demonstrates setting server options for denying server header, disabling response header compression, and configuring runspaces.
    File:    13.2-Server-Options.ps1
#>
param(
    [int]$Port = 5000,
    [IPAddress]$IPAddress = [IPAddress]::Loopback
)
# 1. (Optional) Logging pipeline so we see events
New-KrLogger |
    Set-KrLoggerLevel -Value Debug |
    Add-KrSinkConsole |
    Register-KrLogger -Name 'console' -SetAsDefault | Out-Null

# 2. Create server host
New-KrServer -Name 'Server Limits'

# 3. Add loopback listener on port 5000 (auto unlinks existing file if present)
# This listener will be used to demonstrate server limits configuration.
Add-KrEndpoint -Port $Port -IPAddress $IPAddress


# 5. Set server options for denying server header, disabling response header compression, and configuring runspaces
Set-KrServerOptions -DenyServerHeader -MaxRunspaces 8 -MinRunspaces 2

# 6. Finalize configuration and set server limits
Enable-KrConfiguration

# 7. Map a simple info route to demonstrate server options in action
Add-KrMapRoute -Verbs Get -Pattern '/info' -ScriptBlock {
    Write-KrJsonResponse @{ status = 'ok'; time = (Get-Date) }
}

# 8. Start the server (runs asynchronously; press Ctrl+C to stop)
Start-KrServer

Step-by-step

  1. Create server + listener (loopback:5000).
  2. Add runtime.
  3. Apply options (-DenyServerHeader, runspace pool sizing, etc.).
  4. Enable configuration (commits listeners + runtime + options).
  5. Map /info route returning status/time.
  6. Start server.

Common flags

Option Purpose
-DenyServerHeader Remove Server header
-AllowSynchronousIO Permit legacy sync IO (avoid unless required)
-DisableResponseHeaderCompression Debug proxies / diagnostics
-MaxRunspaces / -MinRunspaces Script concurrency tuning

Try it

Show removal of the Server response header when using -DenyServerHeader.

# With -DenyServerHeader (no Server header expected)
Invoke-WebRequest -Uri http://127.0.0.1:5000/info | Format-List StatusCode,Headers,Content

# For comparison restart WITHOUT -DenyServerHeader then:
Invoke-WebRequest -Uri http://127.0.0.1:5000/info | Select-Object -ExpandProperty RawContent

Expected (with -DenyServerHeader): headers DO NOT include Server: Kestrel.

Expected (without): response contains a Server: Kestrel header.

References

Troubleshooting

Symptom Cause Fix
Still see Server header Option applied after start Apply before Start-KrServer
High CPU Too many runspaces Reduce -MaxRunspaces
Slow throughput Too few runspaces Increase -MaxRunspaces

See also: Server Limits · Logging


Previous / Next

Previous: Server Limits Next: Demos