Nginx regex vhost pattern ends up as PHP server name

Solution 1:

Thanks to the rubber-duck effect of writing this question, I found a solution.

Nginx's stock fastcgi_params file contains the line:

fastcgi_param  SERVER_NAME        $server_name;

which is what causes that value to appear in $_SERVER['SERVER_NAME'] in the PHP environment.

I changed that to use the $host variable:

fastcgi_param  SERVER_NAME        $host;

and my problem went away. I'd be interested to know if there are any downsides of this approach.

Solution 2:

server_name  ~^(?<subdomain>.+)\.example\.com$;
set $server_name_full $subdomain.example.com;


location ~ \.php$ {
    ...
    include fastcgi_params;
    fastcgi_param SERVER_NAME $server_name_full;
    ...
}