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

Rewrite to fastcgi #67

Open
tvlooy opened this issue Nov 16, 2016 · 6 comments
Open

Rewrite to fastcgi #67

tvlooy opened this issue Nov 16, 2016 · 6 comments

Comments

@tvlooy
Copy link

tvlooy commented Nov 16, 2016

I want httpd to work like this:

In Nginx you do that with a :

    location / {
        try_files $uri /index.php$is_args$args;
    }

In Apache you would do that with a:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

I think this is not possible with httpd right now and I am trying to make a patch to support this.
This would make a lot of PHP CMS'es, frameworks, ... work

@miraculli
Copy link

there is diff to achieve that.
https://marc.info/?l=openbsd-tech&m=148094767011911&w=2

i never tested it.

@marianielias
Copy link

Can't this be done now with rewrite?

@hexadecatrienoic
Copy link

hello, apologies for the necro. is this patch still the way to do this, or is it possible with the current rewrite?

@marianielias
Copy link

hello, apologies for the necro. is this patch still the way to do this, or is it possible with the current rewrite?

I'm not sure, check it out, the documentation in OpenBSD is really complete:
https://man.openbsd.org/httpd.conf#request
https://man.openbsd.org/httpd.conf#EXAMPLES

@o-alquimista
Copy link

o-alquimista commented Aug 2, 2021

Using the Symfony 5 documentation as a reference, I've come up with this:

server "example.org" {
        listen on * port 80
        alias www.example.org
        root "/htdocs/example/public/"
        directory index index.php

        location found "/index.php" {
                fastcgi socket "/run/php-fpm.sock"
        }

        location found "*.php" {
                block return 403
        }

        location not found "*" {
                request rewrite "/index.php"
        }
}

I've tested it thoroughly to catch any edge cases, and it seems pretty solid.

Explanation for the location blocks:

  1. Only index.php (the front controller) goes to fastcgi.
  2. Any other PHP file is blocked
  3. Anything else not found, redirect to index.php.

UPDATE: it's missing one thing:

After you deploy to production, make sure that you cannot access the index.php script (i.e. http://example.com/index.php).

That is recommended to conceal that you're running PHP.

@o-alquimista
Copy link

o-alquimista commented Mar 12, 2022

New and improved :)

# application front controller
location found "/index.php" {
  fastcgi socket "/run/php-fpm.sock"
}

# fallback to front controller if the location doesn't exist
location not found "*" {
  request rewrite "/index.php"
}

# fallback to front controller if the location is an existing directory
location found "**/" {
  request rewrite "/index.php"
}

# if the requested path doesn't match any of the locations above,
# httpd will just serve the resource (e.g. images, css, js, etc.)

This handles some edge cases the older one didn't, like when you try to access an existing directory (e.g. /css/) and httpd spits out an ugly 404 response page.

I just haven't yet tested it with directory auto index enabled. I'm not sure if it's possible for us to handle this nicely. A feature request for httpd to do this for us would be nice.

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

No branches or pull requests

5 participants