Caddy reverse_proxy and upstream timeout

I am configuring Caddy reverse proxy for my Python application backend. However, sometimes Python backend (Waitress) can be slow to respond if the underlying database is overloaded. How can I configure reverse_proxy to timeout on too slow requests? I see multiple timeout options in Caddy documentation. However, I could not find any written insight on how these should be used.

My assumption is that if the backend does not write any response headers, then response_header_timeout is the correct timeout option to use. Currently, I am configuring my reverse proxy as following and I want to confirm it is what I hope it to be.

Also, what HTTP response Caddy will send if the reverse proxy upstream timeouts?

    # Backend API request
    handle /api* {
        # This is the upstream Waitress server
        reverse_proxy 127.0.0.1:3456 {
            header_up X-Real-IP {remote_host}

            # Backend API must respond to an individual API call under 20 seconds
            transport http {
                response_header_timeout 20s
            }
        }
    }

My Caddyfile.

Well, you can easily test it by making your upstream do a sleep(30) or something to make it wait longer than 20 seconds.

I think that timeout is probably the right thing to use. I think if a timeout happens, a 502 will be written. You can customize that behaviour by using the handle_errors directive, which lets you re-handle a failed request with something else, such as serving a static error page from file (with root + file_server)

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.