Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastcgi: Add new php_fastcgi subdirectives to override shortcut behaviour #3255

Merged
merged 9 commits into from
May 18, 2020

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Apr 11, 2020

The php_fastcgi Caddyfile directive acts as a shortcut to a quite long expanded form. There are many situations where users might want to override some of the behaviour generated by the shortcut.

This change makes it possible to override some aspects of the shortcut by first reading the subdirective block once, handling any subdirectives that are relevant, and importantly, deleting the tokens as it goes. Then, it rewinds the Dispenser's cursor to the point just before so that the reverse_proxy's UnmarshalCaddyfile can read it for itself. We delete as we go, because otherwise the default: case in reverse_proxy would return d.Errf("unrecognized subdirective %s", d.Val()).

Adds the following subdirectives:

  • split: passthrough to the split_path subdirective of the fastcgi transport, but also affects the path matcher for the list of PHP file extensions to support
  • env: passthrough to the env subdirective of the fastcgi transport
  • root: passthrough to the root subdirective of the fastcgi transport
  • index: defaults to index.php, controls the file that is used as a try_files fallback. If the value is set to off, it disables the path canonicalization redirect and the try_files rewrite altogether.

Caddyfile:

:8884

php_fastcgi localhost:9000 {
        # some php_fastcgi-specific subdirectives
        split .php .phpt
        env PATH /something
        root /var/somewhere
        index off

        # passed through to reverse_proxy (directive order doesn't matter!)
        lb_policy random
}

JSON output:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":8884"
          ],
          "routes": [
            {
              "match": [
                {
                  "path": [
                    "*.php",
                    "*.phpt"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "reverse_proxy",
                  "load_balancing": {
                    "selection_policy": {
                      "policy": "random"
                    }
                  },
                  "transport": {
                    "env": {
                      "PATH": "/something"
                    },
                    "protocol": "fastcgi",
                    "root": "/var/somewhere",
                    "split_path": [
                      ".php",
                      ".phpt"
                    ]
                  },
                  "upstreams": [
                    {
                      "dial": "localhost:9000"
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

@francislavoie francislavoie requested a review from mholt April 11, 2020 01:34
@francislavoie
Copy link
Member Author

francislavoie commented Apr 13, 2020

I was reading through the codebase some more and I realized there's a better approach than adding 2 new funcs to Dispenser to manipulate the cursor.

I'm pretty sure I can use NewFromNextSegment(), mutate that new Dispenser as I currently do, then Reset() it and pass it down to reverse_proxy's unmarshal.

I'll refactor this soon.

Edit: Done

@mholt mholt added the under review 🧐 Review is pending before merging label Apr 14, 2020
@mholt mholt added this to the 2.1 milestone Apr 15, 2020
@francislavoie francislavoie changed the title fastcgi: Add new php_fastcgi subdirectives to override shortcut behaviour fastcgi: Add new php_fastcgi subdirectives to override shortcut behaviour Apr 19, 2020
@mholt
Copy link
Member

mholt commented May 11, 2020

@francislavoie When you get a chance, could you fix the merge conflict? Then I'll do a final review. Looking good though.

Note that I'll be relying on you for fixes to any potential bug reports that come in related to this in the future 😅 (esp. since I don't really have a PHP environment!)

Thanks!

@francislavoie
Copy link
Member Author

Yep, this one will need a bit of reworking actually, because of the split_path thing we added.

@mholt
Copy link
Member

mholt commented May 11, 2020

Gotcha... no hurry. I can see the huge value in this though. Thanks!

@francislavoie francislavoie force-pushed the php-fastcgi-options branch 2 times, most recently from 8a4bad3 to 9c4592f Compare May 12, 2020 00:14
@mholt mholt linked an issue May 12, 2020 that may be closed by this pull request
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! It'll be super useful.

I haven't verified that all the hidden/implicit route logic was brought over correctly, but I trust that it works.

Just a couple minor nits -- nothing showstopping though. Would be nice if a few more people with actual PHP environments could test this out first?

modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go Outdated Show resolved Hide resolved
modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go Outdated Show resolved Hide resolved
@francislavoie francislavoie force-pushed the php-fastcgi-options branch from 9c4592f to 82ba899 Compare May 17, 2020 03:07
@francislavoie francislavoie requested a review from mholt May 17, 2020 03:07
@francislavoie francislavoie force-pushed the php-fastcgi-options branch from 82ba899 to 89de0fc Compare May 18, 2020 18:04
@mholt mholt removed the under review 🧐 Review is pending before merging label May 18, 2020
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Code looks nice, I'll assume it works. Anyone with a PHP environment could please test it out, thank you!

@mholt mholt merged commit 7243454 into caddyserver:master May 18, 2020
@francislavoie francislavoie deleted the php-fastcgi-options branch May 18, 2020 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

php_fastcgi: allow setting root
2 participants