Wildcard domains fastcgi

Hi i am using caddy for simple development server. I am refering mostly to this post Wildcard virtual domains with wildcard roots

I have tried everything i can think of… this config works nice:

*.domain.com:80 {
  tls off
  root /var/www/domain.com/subdomains
  log /var/www/log/domain.log
  errors {
    * /var/www/errors/default_error.html
  }

   rewrite {
     to {host}{uri}
  }
}

This runs static files just fine. I need it to run fastcgi but i cant make it work.

*.domain.com:80 {
  tls off
  root /var/www/domain.com/subdomains
  log /var/www/log/domain.log
  errors {
    * /var/www/errors/default_error.html
  }

  fastcgi / /run/php/php7.0-fpm.sock php

  rewrite {
    to {uri}/index.php?{path}&{query}
  }

}

In the rewrite i tried any combination of {host}{uri}, {hostonly} all kinds of stuff. But what i get is that script runs renders page fine but paths of static files are wrong.

URL: http://example.domain.com/

Served site has links to subpages and static files like this:
http://.domain.comexample.domain.com/subpage
http://.domain.comexample.domain.com/style.css

The php site has its own router so its possible that the problem is there but i tried to haggle with that one too and it does not make any difference. IDK. Basically i just need rewrite everything subdomain.example.com to /var/www/subdomain.example.com and have fastcgi working

Its probably very specific example but i am so close! If anybody has any ideas about this… THANKS IN ADVANCE

I’m really not 100% sure what you’re trying to do here, but rewriting to {uri}/index.php?{path}&{query} seems pretty odd. A typo? For a request to example.com/foo/bar?a=b, this rewrite would translate to example.com/foo/bar?a=b/index.php?/foo/bar&a=b.

I’m also doubtful that http://.domain.comexample.domain.com/ is intentional.

I note that your site root is /var/www/domain.com/subdomains, but you want to rewrite to /var/www/subdomain.example.com. This won’t be possibe, because you want to rewrite to a location outside the webroot.

This might be doable if your webroot was /var/www and you rewrote to /{host}{uri}, which would translate a request for example.com/foo/bar to example.com/example.com/foo/bar, which would pull static files from /var/www/example.com/foo/bar. As long as your PHP files are in these subfolders as expected, fastcgi / /run/php/php7.0-fpm.sock php should work just fine.

Let me know if I’ve misunderstood.

Thank you!

I’ve explaned myself badly. I am stupid i dont need {uri}. I just need rewrite to subfolder/index.php. Let me put it in different way… This if i do it manualy this works properly:

test.example.com:80 {
  tls off
  root /var/www/example.com/subdomains/test.example.com

  fastcgi / /run/php/php7.0-fpm.sock php
  rewrite {
    to {uri} /index.php?{path}&{query}
  }
}

But i cant seem to get something like this to work with wildcard.

*.example.com:80 {
  tls off
  root /var/www/example.com/subdomains/

  fastcgi / /run/php/php7.0-fpm.sock php
  rewrite {
    to {host}/index.php?{path}&{query}
  }
}

Actualy it is working page works but all the links end up wrong like this:

http://.example.comtest.example.com/

Php doesnt get the proper urls. Maybe the fastcgi passes *.example.com to php instead of the test.example.com?

Maybe its problem with php? Probably i just have the config set improperly.

OK i dont know why but fastcgi does not seem to get right url (it seems to get url before wildcard is aplied). PHP seems to think its url is *.example.com.

Anyways i’ve solved it in differend way using wildcard imports and file watching using incron which reloads caddy.

It might even bee better solution (caddyfile for each subdomain).

Thank you Whitestrake! You are very helpful.

1 Like

I’m not too incredibly familiar with the inner workings of FCGI - I’ll be honest, I just use the magic words everyone else says to use (fastcgi / php-fpm:9000 php) and vodoo magic happens and my page gets rendered! :smiley:

But I think I’m seeing part of the problem. You’re rewriting the URI to {host}/index.php?..., and leaving out the initial forward slash the URI usually begins with (i.e. /{host}/index.php?...). So It’s jamming {host} (which Caddy gets from the request headers as test.example.com) on the end of the server name (*.example.com) and you get .example.comtest.example.com via simple substitution.

FastCGI doesn’t really care about it with regards to rendering the page, because Caddy’s already figured out the details of the request and what to serve, so it just processes the PHP it gets told to process. But I’m guessing you’ve got some references to the server name in your PHP to generate the links, so that’s what you’re seeing?

Anyway, glad you’ve got another solution!

I could not find the reason for this.

I thought about initial forward slash too but /{host} or {host} gave me same results.
I also tried to mess with server name in the php app but it messed up links in different way.

Anyways for future reference - it works if you dont use centralised router (which almost every php app nowdays does). Otherwise you can sort of make similar functionality to .htacess files using caddy import and filewatching on your system (i use incron on linux). Every modification of file reloads kirby server.

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