Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In my PHP app:

die($_SERVER['SERVER_NAME']);

The request:

sub1.example.com

The result:

example.com

My NGINX config:

server
{
    server_name .example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    root /var/www/html/example;
    index index.php;

    location ~ \.php$ {
        if (-f $request_filename) {
            fastcgi_pass    127.0.0.1:9000;
        }
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }

    location / {
        if ($request_filename ~ /sub1) {
            rewrite ^ http://sub1.example.com/? permanent;
        }
        if ($request_filename ~ /sub2) {
            rewrite ^ http://sub2.example.com/? permanent;
        }
        if (!-f $request_filename) {
            rewrite ^(.*)$ /index.php last;
        }
    }
}

Why is the sub-domain not being picked up in php?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

$_SERVER['SERVER_NAME'] is the name of the virtual host, which is .example.com in your nginx config. You'll want $_SERVER['HTTP_HOST'] if you want the hostname from the request regardless of the nginx server_name value.

share|improve this answer
    
This worked for me, awsome! I have a quick follow up question I've posted here: serverfault.com/questions/496609/… –  Dan Kanze Apr 5 '13 at 19:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.