configure multiple subdomains with nginx...

July 19, 2014 3.4k views

Hi everyone,

I am having an issue with nginx configuration when it comes to multiple (over 1000) subdomains. Please help, I got stuck ...

I am trying to redirect all of the sites below to the same web root:

  • 1. xyz.com
  • 2. sub1.xyz.com
  • 3. sub2.xyz.com
  • 4. sub3.xyz.com
  • ...

I have:

  1. Ubuntu 14.04, Laravel 4, nginx. Everything is latest version
  2. Added DNS CNAME, where NAME=* and HOSTNAME=@
  3. And finally, my nginx config looks like this:
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;


        root /var/www/laravel/public;
        index index.php index.html index.htm;

        server_name xyz.com  .xyz.com;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
                rewrite ^/(.*)$ /index.php?_url=/$1;
        } 

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
1 Answer

Replace

server_name xyz.com  .xyz.com;

with

server_name xyz.com  *.xyz.com;

and restart nginx.

Have another answer? Share your knowledge.