-2

My wish is to configure my site in a such way that:

  • company.fr
  • collaborator1.company.fr
  • collaborator2.company.fr
  • ...

the domain and associated subdomains point to the same directory root.

Under the hood I'll use Symfony and the content displayed will depends on the domain or the subdomain requested.

To achieve this goal I'm wondering if I can have a single configuration file:

server {
    server_name company.fr collaborator1.company.fr collaborator2.company.fr;
    root /var/www/project/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

or if I need to split it into multiple server blocks with a similar configuration.

Adrien G
  • 101
  • 3
  • 1
    What is the problem you are having? – Michael Hampton Aug 07 '18 at 23:38
  • @MichaelHampton I just want to know the right way to configure nginx to achieve what I explained. I tried a single configuration based on what I saw to not just "ask for the solution"... I don't understand why I've been down-voted – Adrien G Aug 08 '18 at 07:51
  • OK, so what is not working? – Michael Hampton Aug 08 '18 at 12:10
  • @MichaelHampton I was simply asking for feedbacks in order to implement those needs correctly. That's why I created a configuration to show you that I did some research. – Adrien G Aug 08 '18 at 12:39

2 Answers2

0

I would do the same, it's correct.

server_name company.fr collaborator1.company.fr collaborator2.company.fr;
Federico Galli
  • 918
  • 6
  • 16
0

You might consider using a server_name which includes all possible subdomains, if you plan to add them frequently. This way you don't have to change the nginx configuration in order to add a subdomain.

server_name *.company.fr;

This includes all subdomains, but does not include company.fr.

server_name .company.fr;

This includes all subdomains, and does include company.fr. Using this would serve any name in the entire domain from this server.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • this solution seems to be more flexible because I'll be able to throw a 404 (in PHP) if someone tries a non existing subdomain and I won't need to edit the configuration :) – Adrien G Aug 08 '18 at 15:34