Invoke another middleware

Hi,

I’m looking to use Caddy as a frontend for a set of fastcgi servers, with the condition that the selected backend depends on request data such as the host and headers, and I cannot determine ahead of time what those will be.

Ideally, to make the Caddyfile clearer, I’d like to write a plugin that exposes a placeholder, such as:

myplugin {
   some stuff
}

fastcgi / {mypluginbackend} php

But from looking at the code, it seems no replacement takes place when extracting the server address.

Another option, which I’m not sure would work, is to write a pĺugin that actually calls the fastcgi middleware from it, creating the Handler and Rules manually and then calling ServeHTTP, something like:

func (h MyPluginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
    fgciHandler := createFastcgiHandler(r)
    fcgiHandler.ServeHTTP(w, r)
}

About these two options:

  1. For option one, would a Replacer be all that’s needed to get the placeholder expanded for the address? Not sure if this would make a good PR because this might incur some penalty for all fastcgi requests, even those not using replacements. Could also add a new option similar to upstream that would run replacements. There’s already an indirection when getting the server address so a new type of balancer wouldn’t cause issues for existing fastcgi blocks, I believe. Though it increases the module’s complexity
  2. For option two, is that possible? Are there drawbacks/limitations to this approach? With this one I wouldn’t have to modify Caddy itself, but since I’m still new to Caddy I’m not sure if this is a supported pattern

Thanks,
Felipe

Hi @fcoelho, if I recall correctly the FastCGI configurations are generated at Caddyfile parse, including the upstream server(s), and aren’t really dynamic. You can use environmental variables to get it pointed at your backend to start off with, but if it changes at any point after that you’re likely going to need your own custom FastCGI handler that can respond to upstream address changes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.