Nginx and Catch for Sub Domain with PHP


Nginx and Catch for Sub Domain with PHP

在Nginx中,我有一个catch-all设置,如下所示:

server {
    listen   80;
    server_name  *.example.com;
    location / {
        root   /data/sites/www.example.com/widgets/public_html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    location ~ .php$ {
      root          /data/sites/www.examples.com/widgets/public_html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_index  index.php;
      include        fastcgi_params;
    }
}

如果我键入abc.example.com,它就会起作用。现在在PHP中,我需要读取子域。我得到的子域是这样的:

$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$urlSegments = parse_url($url);
$urlHostSegments = explode('.', $urlSegments['host']);
$subdomain = $urlHostSegments[0];

问题是,它不是以abc作为子主体返回,而是返回*。那么,对于PHP和NGINX,我如何获得具有catch-all的实际子域呢?

尝试:

array_shift(explode(".",$_SERVER['HTTP_HOST']));
print_r($_SERVER);

了解右侧变量。

它是HTTP_HOST,当然不是SERVER_NAME