Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In my PHP app:


The request:

The result:

My NGINX config:

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;
    root /var/www/html/example;
    index index.php;

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

    location / {
        if ($request_filename ~ /sub1) {
            rewrite ^ permanent;
        if ($request_filename ~ /sub2) {
            rewrite ^ permanent;
        if (!-f $request_filename) {
            rewrite ^(.*)$ /index.php last;

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

$_SERVER['SERVER_NAME'] is the name of the virtual host, which is 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:… –  Dan Kanze Apr 5 '13 at 19:13

Your Answer


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.