Haskell Web Apps Behind Load Balancers
Haskell web frameworks like Servant and Yesod run on Warp under the hood. When placed behind a load balancer, you need to handle proxy headers and connection management properly.
Warp Proxy Configuration
Warp needs to trust forwarded headers from your load balancer:
import Network.Wai.Handler.Warp
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.ForceSSL
main :: IO ()
main = do
let settings = setPort 8080
$ setHost "0.0.0.0"
$ setTimeout 120
$ defaultSettings
runSettings settings appFor SSL detection behind a proxy, check X-Forwarded-Proto:
import Network.Wai
isSecure :: Request -> Bool
isSecure req =
case lookup "X-Forwarded-Proto" (requestHeaders req) of
Just "https" -> True
_ -> FalseHealth Check Endpoint
With Servant:
type HealthAPI = "healthz" :> Get '[PlainText] Text
healthHandler :: Handler Text
healthHandler = return "ok"Connection Draining
Haskell's lightweight threads mean Warp can handle thousands of connections, but graceful shutdown requires explicit handling:
import Control.Concurrent (threadDelay)
import System.Posix.Signals
main = do
installHandler sigTERM (Catch $ gracefulShutdown) Nothing
runSettings settings app
gracefulShutdown :: IO ()
gracefulShutdown = do
putStrLn "Shutting down gracefully..."
threadDelay 5000000 -- 5 seconds for in-flight requestsBugsly can receive error reports from Haskell backends via its HTTP API, capturing exceptions that might otherwise vanish in container restarts behind the balancer.
Try Bugsly Free
AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.
Get Started FreeRelated Articles
How to Fix Proxy Error in PHP
Learn how to diagnose and fix the proxy error in PHP. Includes code examples and prevention tips.
Read moreHow to Fix Fetch API Network Error in Angular
Learn how to fix the Fetch API Network Error in Angular. Step-by-step guide with code examples.
Read moreFix CORS Blocked Error in C#
Learn how to fix the CORS Blocked error in C#. Step-by-step guide with code examples and solutions. Quick, practical guide for developers.
Read moreFix SSL Error in Ruby
Step-by-step guide to fix SSL Error in Ruby. Includes root cause analysis, code examples, debugging tips, and prevention strategies.
Read more