Thruster Documentation

repository·main·Indexed 22 days ago

https://github.com/basecamp/thruster

An HTTP/2 proxy designed for production-ready Rails deployments. Thruster runs alongside Puma to provide TLS management via Let's Encrypt, HTTP caching, and efficient static file serving. It is distributed as a Ruby gem with a Go-based core and can be configured via environment variables to manage timeouts, compression, and BREACH attack mitigation.

Tokens
1.7K
Snippets
7
Records
9
Agent score
77%

What's inside Thruster

  1. Mitigate BREACH attacks in Thruster

    main

    Thruster provides two mechanisms to protect against the BREACH attack, which targets secrets in compressed, encrypted traffic:

    1. Random Jitter (Default): Thruster adds random padding to the size of compressed responses. This is controlled by the GZIP_COMPRESSION_JITTER environment variable (default is 32 bytes). Setting this to 0 disables jitter.
    2. Compression Guard (Recommended for high security): You can disable compression entirely for requests that contain sensitive authentication headers (Cookie, Authorization, or X-Csrf-Token). This eliminates the side-channel for sensitive traffic.

    To enable the Compression Guard, set: GZIP_COMPRESSION_DISABLE_ON_AUTH=true

  2. Run your application with Thruster

    main

    To use Thruster, prefix your application's startup command with thrust. Thruster acts as a wrapper that manages the Puma process for you, making it ideal for containerized environments where you want a single CMD to handle both the proxy and the web server.

    Basic Usage (HTTP only)

    Run your server normally, prefixed with thrust:

    $ thrust bin/rails server

    Usage with Automatic TLS

    To enable automatic TLS certificate management via Let's Encrypt, you must set the TLS_DOMAIN environment variable to your domain name:

    $ TLS_DOMAIN=myapp.example.com thrust bin/rails server
  3. Install Thruster

    main

    Thruster is distributed as a Ruby gem. Because the core is written in Go, the gem automatically fetches the appropriate platform-specific binary for your system during installation.

    To install it in your Rails application, add it to your Gemfile:

    gem 'thruster'

    Alternatively, you can install it globally on your system using the gem command.

    gem 'thruster'
  4. Configure Thruster via environment variables

    main

    Thruster is designed to be zero-config, but you can customize its behavior using environment variables. To avoid naming collisions with your application, you can optionally prefix any variable with THRUSTER_ (e.g., THRUSTER_TLS_DOMAIN). Prefixed variables take precedence over unprefixed ones.

    Core Configuration

    Variable NameDescriptionDefault
    TLS_DOMAINComma-separated list of domain names for TLS provisioning. If not set, TLS is disabled.None
    TARGET_PORTThe port your Puma server should run on. Thruster sets PORT to this value for the server.3000
    HTTP_PORTThe port to listen on for HTTP traffic.80
    HTTPS_PORTThe port to listen on for HTTPS traffic.443
    STORAGE_PATHPath to store internal state (like provisioned TLS certificates)../storage/thruster
    BAD_GATEWAY_PAGEPath to an HTML file to serve on 502 errors../public/502.html

    Caching and Compression

    Variable NameDescriptionDefault
    CACHE_SIZESize of the HTTP cache in bytes.64MB
    MAX_CACHE_ITEM_SIZEMaximum size of a single item in the HTTP cache in bytes.1MB
    GZIP_COMPRESSION_ENABLEDEnable/disable gzip compression. Set to 0 or false to disable.Enabled
    GZIP_COMPRESSION_DISABLE_ON_AUTHIf true, disables gzip for requests with Cookie, Authorization, or X-Csrf-Token headers.false
    GZIP_COMPRESSION_JITTERRandom jitter (in bytes) added to compressed response size to mitigate BREACH. Set to 0 to disable.32
    X_SENDFILE_ENABLEDEnable/disable X-Sendfile support. Set to 0 or false to disable.Enabled

    Timeouts and Limits

    Variable NameDescriptionDefault
    HTTP_IDLE_TIMEOUTMax seconds a client can be idle before connection closure.60
    HTTP_READ_TIMEOUTMax seconds for client to send request headers and body.30
    HTTP_WRITE_TIMEOUTMax seconds for client to read the response.30
    MAX_REQUEST_BODYMax request body size in bytes. 0 means no limit.0

    TLS and Advanced Settings

    Variable NameDescriptionDefault
    ACME_DIRECTORYURL of the ACME directory for TLS provisioning.https://acme-v02.api.letsencrypt.org/directory
    EAB_KIDEAB key identifier for TLS provisioning.None
    EAB_HMAC_KEYBase64-encoded EAB HMAC key for TLS provisioning.None
    H2C_ENABLEDSet to 1 or true to enable h2c (http/2 cleartext).Disabled
    FORWARD_HEADERSWhether to forward X-Forwarded-* headers.Disabled with TLS; Enabled otherwise
    LOG_REQUESTSLog all requests. Set to 0 or false to disable.Enabled
    DEBUGSet to 1 or true to enable debug logging.Disabled
  5. Initialize and manage a Thruster Server

    main

    The Server type is the primary entrypoint for running the Thruster proxy. It manages both HTTP and HTTPS listeners, handles TLS certificate management via ACME (autocert), and supports automatic HTTP-to-HTTPS redirection.

    To use the server, call NewServer with a *Config and an http.Handler (the backend application or proxy logic), then call Start() to begin listening for requests. Use Stop() to perform a graceful shutdown with a 5-second timeout.

    // Example usage of the Server
    server := internal.NewServer(config, myHandler)
    
    if err := server.Start(); err != nil {
    	log.Fatal(err)
    }
    
    // ... run application ...
    
    server.Stop()
  6. Run the thrust CLI

    main

    The thrust command is the primary entrypoint for the Thruster service. When executed, it initializes a configuration from the environment, sets up a JSON logger at the configured log level, and starts the Thruster service. The service runs and will exit with a non-zero status code if it encounters a fatal error.

    # The command is typically invoked via the compiled binary
    ./thrust
  7. Start the Thruster Server

    main

    The Start() method initializes the network listeners and starts the HTTP and HTTPS servers in separate goroutines.

    • If TLS is configured (config.HasTLS() is true): It sets up an autocert.Manager for automatic certificate acquisition, configures an HTTP redirect handler to force HTTPS, and starts both HTTP and HTTPS listeners.
    • If TLS is not configured: It only starts an HTTP server on the configured HttpPort using the provided handler.
    func (s *Server) Start() error