简体   繁体   中英

Use one Webroot and differentiate between subdomains by ID

I have been tasked with optimizing a web service which provides photos on a per-customer basis. So customer A can use customerA.domain.com and get presented with his own images on a sleek page.
Currently, every subdomain is rooted in each own web root (with its respective index.php, etc.). Now I am to unify these websites so that only one webroot is being used, with the subdomain being the deciding factor for which set of photos (organized in numbered folders) is to be used. This is to maintain the expandability and changeability of the website.

In order for this to work, I thought of the following conditions that need to be met:

  • Nginx needs to route a limited set of subdomains to the same webroot
  • Depending on the subdomain, the webpage (PHP) must be aware of a predefined ID, needed for Database operations specific to a customer (subdomain) and finding the correct directory of images.

Partial (possible?) solutions I have come up with:

  • Store the subdomain in the customer table in the DB, parse the subdomain from the HTTP header and get the ID from the DB
  • Configure Nginx to append the correct ID to the URL (as a GET parameter)

An example of one customers nginx config, fairly default.

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; 
}

How would I modify nginx config to prepend a GET-parameter with a static ID, if possible? How do I funnel all specified subdomains to the same webroot?

Now, it is entirely possible that I have simply made the wrong search queries, for I have found no solutions (neither Google nor SO) for my question, and I can't imagine I'm the first one having a problem of the kind.

Thank you for your help.

Here's how you can do it in the two ways you mentioned:

Here you'd have a server block for each customer, manually setting the userid variable to each one as necessary.

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'];

?>

or

Here you'd have one server block for each customer, and you'd have PHP do the work to find out the user 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

?>

If you only have a few subdomains, the first is probably better. Otherwise the latter option would be the best to prevent having a huge nginx configuration and work out of the box for customer growth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM