nginx "server" directive with multiple "server_name" entries: always first one is passed to PHP's $_SERVER['SERVER_NAME']

Paolo picture Paolo · Jul 17, 2015 · Viewed 30.3k times · Source

My configuration file has a server directive block that begins with...

server {
    server_name www.example1.com www.example2.com www.example3.com;

...in order to allow the site to be accessed with different domain names.

However PHP's $_SERVER['SERVER_NAME'] always returns the first entry of server_name, in this case http://www.example1.com

So I have no way from the PHP code to know which domain the user used to access the site.

Is there any way to tell nginx/fastcgi to pass the real domain name used to access the site?


The only solution I've found so far is to repeat the entire server block for each domain with a distinct server_name entry but obviously I'm looking for a better one.

Answer

Tan Hong Tat picture Tan Hong Tat · Jul 22, 2015

Set SERVER_NAME to use $host in your fastcgi_params configuration.

fastcgi_param   SERVER_NAME         $host;

Source: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_param