Nginx和Catch for Sub Domain with PHP

时间:2013-08-12 12:31:28

标签: php nginx

在Nginx中,我有一个像这样的设置:

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的实际子域名?

2 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:1)

print_r($_SERVER);

知道正确的变量。

HTTP_HOST,当然不是SERVER_NAME