Search code examples
nginxserver-namehttp-host

Nginx propagate server_name regex inside Php


In my Nginx configuration there’s

server_name ~^(?<domain>.*mygreatsite987778.com.invalid)$ ;

Which works quite good. But When Php does echo $_SERVER['SERVER'], it sees:

~^(?.*mygreatsite987778.com.invalid)$

Despite it seems logical, is there a way to get the server name that matched the regex? The server variable HTTP_HOST contains the good value (stuff.mygreatsite987778.com.invalid). I’d like to keep the SERVER variable.


Solution

  • Those variables are defined in your Nginx configuration file. You can set them to any value you require.

    The variable you are looking at (which on my system is called SERVER_NAME) is using the value of the $server_name variable, but you would prefer to use the value of the $host or $http_host value.

    Most of these variables are defined in a file called fastcgi_params, but you can override them individually by placing a statement after the include statement.

    For example:

    include       fastcgi_params;
    fastcgi_param SERVER $http_host;