Nginx: regex on server_name post

Quick tip to use regex on nginx's server_name directive.

Using a specific root directory for every subdomain:

server {
    listen 80;
    server_name ~^(.*)\.project\.com$;
    root /home/www/project/$1;
}

Using an environment variable with a single project:

server {
    listen 80;
    server_name ~^(.*)\.project\.com$;
    fastcgi_param CUSTOMER $1;
    root /home/www/project;
}

You can also use the entire domain name with this:

server {
    listen 80;
    server_name ~^(.*)\.(.*\..*)$;
    root /home/www/$2/subdomains/$1/public_html;
}

Tested with Nginx 1.4.x.

Tags: nginx

Comments

comments powered by Disqus