使用一个 Webroot 并通过 ID 区分子域

Use one Webroot and differentiate between subdomains by ID

我的任务是优化一个网络服务,该服务为每个客户提供照片。因此客户 A 可以使用 customerA.domain.com 并在一个时尚的页面上展示他自己的图像。
目前,每个子域都植根于每个自己的网络根目录(及其各自的 index.php,等等)。现在我要统一这些网站,以便只使用一个 webroot,子域是决定使用哪组照片(组织在编号文件夹中)的决定因素。这是为了保持网站的可扩展性和可变性。

为了让它起作用,我想到了需要满足以下条件:

我想出的部分(可能?)解决方案:

一个客户的 nginx 配置示例,相当默认。

server {
    listen 80; ## listen for ipv4; this line is default and implied
    listen 443; ## listen for ipv4; this line is default and implied
    ssl on;

    ssl_certificate /mnt/www-cluster22/scripts/cert/2018/thedomain.crt;
    ssl_certificate_key /mnt/www-cluster22/scripts/cert/2018/privatekey.key;

    root /mnt/www-cluster22/foto_cms/32;
    index index.php index.html index.htm;
    server_name customerA.thedomain.com;
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    access_log /var/log/nginx/customerA.access.log;
    error_log /var/log/nginx/customerA.error.log; 
}

如果可能,我将如何修改 nginx 配置以在 GET 参数前加上静态 ID?如何将所有指定的子域集中到同一个 webroot?

现在,我完全有可能简单地进行了错误的搜索查询,因为我没有找到解决我的问题的方法(Google 和 SO),而且我无法想象我是第一个遇到这种问题。

感谢您的帮助。

您可以按照您提到的两种方式进行操作:

在这里,每个客户都有一个服务器块,根据需要手动为每个客户设置 userid 变量。

server {
    ...
    server_name customerA.thedomain.com;
    location ~ \.php$ {
        set $args $args&userid=123; # Here's how to append a GET variable
        ...
    }
    ...
}

PHP:

<?php

$user_id = $_GET['userid'];

?>

在这里,每个客户都有一个服务器块,并且 PHP 执行查找用户 ID 的工作。

server {
    ...
    server_name customerA.thedomain.com customerB.thedomain.com customerC.thedomain.com;
    ...
}

PHP:

<?php

$hostname = $_SERVER['HTTP_HOST'];
$customer = substr($hostname, 0, strpos($hostname, "."));

// Lookup the User ID for the $customer in the database

?>

如果您只有几个子域,第一个可能更好。否则,后一种选择将是防止拥有庞大的 nginx 配置并开箱即用以促进客户增长的最佳选择。