Multiple URLs in nginx kirby configuration

Hi all,

Something that has been bugging me for a while… All works well with nginx and Kirby except when I add multiple domain aliases in the same server_name. Then kirby always defaults to the first alias. (making it impossible to use the site with a second alias)

If I add the alias as a 2nd “server” it works as expected. It’s mainly a pain when you have a lot of domain aliases. By having separate server items you need to do your changes for every separate one. (or when you add a previewlink before the site is live, it always default to the live link then)

So for example, this always defaults to mywebsite.com (and panel is not accessible via mywebsite.staging.com when mywebsite.com doesn’t exist yet)

server {
    listen 80;
    listen [::]:80;

    root /home/mywebsite/mywebsite.com/;
    server_name mywebsite.com mywebsite.staging.com;
    ....
}

This does work as expected

server {
    listen 80;
    listen [::]:80;

    root /home/mywebsite/mywebsite.com/;
    server_name mywebsite.com;
    ....
}

server {
    listen 80;
    listen [::]:80;

    root /home/mywebsite/mywebsite.com/;
    server_name mywebsite.staging.com;
    ....
}

I wonder if I’m the only one having this problem and if its even solvable or if it’s just how kirby works with multiple domains.

Cheers!
S

Kirby is using $_SERVER['SERVER_NAME'] (check this Quicktip: Serving a page on its own domain | Kirby)

Nginx by default passes first host as server_name two solutions to this here

You could probably do it in index.php by setting $_SERVER['SERVER_NAME'] to $_SERVER['HTTP_HOST']

1 Like

Very good find! I’m going to test this a bit. If the nginx fastcgi setting works that would be awesome. I’ll update my findings when I’ve tested this. Many thx @krisa !