0

I have a slim application that I would like to place under a directory path on a domain. Currently I have it working fine using the default slim file provided, but I would like to put a rewrite rule.

The rewrite rule I want is that only any url prefixed with /foobar/ is accepted, with the /foobar/ stripped away before it hits slim. Here is the equivalent rewrite rule in my apache configuration which I was previously using, and worked exactly how I wanted it:

RewriteEngine On
RewriteRule ^/foobar(/.*) /foobar/public/index.php$1 [QSA,L]

The way we have it working with this rewriterule is that the $_SERVER['REQUEST_URI'] parameter remains the full "http://localhost/foobar/test", but the path (on which Slim matches the route) is just "/test", so we don't need to explicitly create a route group.

My nginx configuration looks like this at the moment:

server {
    listen 80;
    server_name  www.mysite.com mysite.com;
    root /var/www/mysite/foobar/public;

    # This is what I have tried adding, to no avail
    rewrite ^/foobar(/.*) /index.php$1 break;

    try_files $uri /index.php;

    location /index.php {
        fastcgi_connect_timeout 3s;
        fastcgi_read_timeout 10s;
        fastcgi_pass app:9000;     # "app" is a docker container

        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

I have been trying various rules for hours now. I'm not really sure what I'm doing, obviously misunderstanding how nginx works, and I'm sure an expert in nginx knows the right answer already. If there is anyone that can provide information as to where I am going wrong I would hugely appreciate it!

Thanks in advance!

Updated attempt:

server {
    listen 80;
    server_name  www.mysite.com mysite.com;
    root /var/www/mysite;
    index index.php;

    location ~ ^/foobar(/.*)$ {
        root /var/www/mysite/foobar/public;

        fastcgi_connect_timeout 3s;
        fastcgi_read_timeout 10s;
        fastcgi_pass app:9000;

        include fastcgi_params;
        fastcgi_param PATH_INFO $1;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}
Raiden616
  • 119
  • 3
  • 9

1 Answers1

0

You are right, nginx has different logic with redirects.

This approach might work for you. However, I haven't used nginx with software that uses patterns like this, so I'm not sure if this will work. Also, this might not be the optimal way of performing this.

location ~ ^/foobar(/.*)$ {
    fastcgi_connect_timeout 3s;
    fastcgi_read_timeout 10s;
    fastcgi_pass app:9000;     # "app" is a docker container

    include fastcgi_params;
    fastcgi_param PATH_INFO $1;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • Unfortunately that didn't work, but I feel like we're getting closer. That configuration as it is successfully routes to index.php ONLY when the URL begins with /foobar, but the route passed to Slim is just `/`. I tried changing the 8th line to `fastcgi_param SCRIPT_FILENAME $document_root/index.php$1;` (added the $1), and now it passed the whole `/foobar/whatever` to Slim. So is there a variable that is just the `(/.*)` bit inside the location block? – Raiden616 Dec 23 '17 at 10:43
  • I think this was my mistake in the original line, the second `PATH_INFO` overwrote the earlier `PATH_INFO` which should be the correct one. – Tero Kilkanen Dec 23 '17 at 10:52
  • Hmm same result unfortunately... I changed the confguration a little since I posted the question to try and get it to work, so not sure if something I did conflicted with your answer? I've posted what I have at the moment (after applying your suggestion) in the original question. – Raiden616 Dec 23 '17 at 10:56