Skip to content

Deploying ASGI apps under Hypercorn in production

Run an ASGI app under Hypercorn with HTTP/2, HTTP/3 (QUIC), and systemd socket activation, with worker tuning and Nginx termination for production.

Hypercorn is the ASGI server you reach for when you need HTTP/2 server push, native HTTP/3 (QUIC), or trio as the underlying async runtime. Uvicorn handles HTTP/1.1 and WebSockets fine, but it speaks HTTP/2 only behind a Nginx terminator — Hypercorn speaks HTTP/2 and HTTP/3 directly. This article wires Hypercorn under systemd with socket activation, picks worker counts deliberately, and terminates TLS either at Nginx or at Hypercorn itself.

How to verify

Confirm the version, the protocol it negotiated, and the worker shape:

hypercorn --version
systemctl status app-hypercorn --no-pager | head
curl --http2 -v -k https://127.0.0.1:8443/health 2>&1 | grep -E '^(>|<|\*) (HTTP|ALPN)'
curl --http3 -v -k https://127.0.0.1:8443/health 2>&1 | grep -i quic
ps -eo pid,ppid,cmd | grep hypercorn | head

If --http3 reports option requires curl built with HTTP/3 support, the local curl can’t test QUIC — use Chrome’s chrome://net-internals/#http3 against a real cert, or quiche-client.

What’s happening

Hypercorn is an ASGI server written by the Quart author with three transport layers: h11 for HTTP/1.1, h2 for HTTP/2, and aioquic for HTTP/3. Each worker is a single OS process running an event loop (asyncio by default, optionally trio), so worker count is set by CPU cores in the same way you’d set Gunicorn-managed Uvicorn workers.

The reason to reach past Uvicorn is protocol scope. HTTP/2 multiplexes many requests on one TCP connection — important for backends serving fan-out of small API calls from a single client. HTTP/3 over QUIC eliminates head-of-line blocking entirely and runs over UDP, which matters for mobile clients on lossy networks. Terminating these at Hypercorn (rather than Nginx) lets the app see the protocol version via scope['http_version'] and choose response encoding accordingly.

The systemd socket activation pattern lets Hypercorn bind to a privileged port (443) without running as root — systemd opens the socket as root, hands the FD to the unprivileged Hypercorn process, and the app inherits an already-bound listener.

The procedure

  1. Install Hypercorn with the protocol extras into the venv:

    /opt/venvs/app/bin/pip install 'hypercorn[h3]' trio
    /opt/venvs/app/bin/hypercorn --version

    hypercorn[h3] pulls aioquic and cryptography — the QUIC stack. trio is optional; only install it if you’ve written your app against trio (Starlette/FastAPI default to asyncio).

  2. Write /etc/hypercorn/app.toml:

    bind = ["unix:/run/app.sock"]
    workers = 4
    worker_class = "asyncio"
    keep_alive_timeout = 75
    graceful_timeout = 30
    accesslog = "-"
    errorlog = "-"
    loglevel = "info"
    # Only set these if Hypercorn terminates TLS itself:
    # certfile = "/etc/ssl/private/app.crt"
    # keyfile  = "/etc/ssl/private/app.key"
    # quic_bind = ["0.0.0.0:443"]
  3. Create the systemd unit at /etc/systemd/system/app-hypercorn.service:

    [Unit]
    Description=ASGI app on Hypercorn
    After=network.target
    
    [Service]
    User=deploy
    Group=www-data
    WorkingDirectory=/var/www/app
    RuntimeDirectory=app
    RuntimeDirectoryMode=0750
    EnvironmentFile=/etc/app/env
    Environment=PYTHONUNBUFFERED=1
    ExecStart=/opt/venvs/app/bin/hypercorn -c file:/etc/hypercorn/app.toml app.main:app
    ExecReload=/bin/kill -s HUP $MAINPID
    KillMode=mixed
    Restart=on-failure
    RestartSec=2
    
    [Install]
    WantedBy=multi-user.target
  4. Start and verify the socket answers:

    sudo systemctl daemon-reload
    sudo systemctl enable --now app-hypercorn
    curl --unix-socket /run/app.sock http://localhost/health
    journalctl -u app-hypercorn -n 80 --no-pager
  5. In Nginx, terminate TLS and proxy to the socket. To preserve HTTP/2 client-to-Nginx and HTTP/1.1 Nginx-to-Hypercorn (the standard split):

    server {
      listen 443 ssl http2;
      listen [::]:443 ssl http2;
      listen 443 quic reuseport;
      listen [::]:443 quic reuseport;
      server_name app.example.com;
      add_header Alt-Svc 'h3=":443"; ma=86400';
    
      location / {
        proxy_pass http://unix:/run/app.sock;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
      }
    }
  6. If you want Hypercorn to terminate TLS itself (no Nginx in front, edge case), use systemd socket activation so it can bind 443 without root:

    # /etc/systemd/system/app-hypercorn.socket
    [Unit]
    Description=Hypercorn listening socket
    
    [Socket]
    ListenStream=443
    BindIPv6Only=both
    
    [Install]
    WantedBy=sockets.target

    And add --bind fd://0 in ExecStart, removing bind from the TOML. systemd passes the FD as stdin.

Common pitfalls

  • HTTP/3 requires a real certificate and UDP/443 reachable from clients — self-signed certs and corporate firewalls that drop UDP both kill QUIC silently. Test against a public hostname before declaring it broken.
  • worker_class = "trio" only works if every route, every dependency, and every library you call is trio-compatible. Mixing trio workers with an asyncio-built app raises RuntimeError at startup.
  • The keep_alive_timeout default of 5 seconds is too aggressive for HTTP/2 — connections multiplex requests over minutes, and a 5-second idle window closes them mid-stream. Set it to 75s as above.
  • Hypercorn doesn’t have max_requests worker recycling. If you’ve seen leaks, pair a daily systemctl reload cron with the unit, or front it with Gunicorn (which adds latency for marginal benefit on async apps).
  • Alt-Svc advertises QUIC to clients — without it, browsers will not try HTTP/3 even if you’ve configured Nginx to listen on UDP/443.

For Uvicorn-based deployments where HTTP/1.1 and WebSockets are sufficient, see Deploying FastAPI under Uvicorn workers. Stack Harbor’s managed operations practice reaches for Hypercorn when a client explicitly needs HTTP/2 server push or HTTP/3 at the origin; everywhere else, Uvicorn behind Nginx is the default ASGI shape because it has more battle-tested operational tooling.