Js Lim
Js Lim

Reputation: 3675

How to exclude a specific subdomain from wildcard domain (Nginx)

For example, I have a domain example.com, the web app will create subdomain when user sign up.

But I want to create a testing environment, e.g. demo.example.com which will point to another project. When user sign up, it will have

Currently what I have

| Name           | Type  | Value           |
|----------------|-------|-----------------|
| example.com.   | A     | 123.123.123.123 |
| *.example.com. | CNAME | example.com.    |

And my nginx config

server {
    listen 80;

    root /var/www/example.com/public;
    index index.html index.htm index.php;
    server_name example.com *.example.com;

    location / {
       try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

How can I create 1 for testing environment?

Upvotes: 3

Views: 3532

Answers (1)

RaviTezu
RaviTezu

Reputation: 3125

You will need to create a new server block with subdomain fqdn as server_name and nginx will follow this order of precedence.

  1. Exact name
  2. Longest wildcard name starting with an asterisk, e.g. “*.example.org”
  3. Longest wildcard name ending with an asterisk, e.g. “mail.*”
  4. First matching regular expression (in order of appearance in a configuration file)

For more info.: http://nginx.org/en/docs/http/server_names.html

Upvotes: 3

Related Questions