Wildcard vhosts on Nginx

77,430

Solution 1

I shall show you.

The configuration file

server {
  server_name example.com www.example.com;
  root www/pub;
}

server {
  server_name ~^(.*)\.example\.com$ ;
  root www/pub/$1;
}

Test files

We have two test files:

$ cat www/pub/index.html 
COMMON

$ cat www/pub/t/index.html 
T

Testing

Static server names:

$ curl -i -H 'Host: example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:42 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Wed, 23 Mar 2011 07:56:24 GMT
Connection: keep-alive
Accept-Ranges: bytes

COMMON

$ curl -i -H 'Host: www.example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:48 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Wed, 23 Mar 2011 07:56:24 GMT
Connection: keep-alive
Accept-Ranges: bytes

COMMON

And regexp server name:

$ curl -i -H 'Host: t.example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:54 GMT
Content-Type: text/html
Content-Length: 2
Last-Modified: Wed, 23 Mar 2011 07:56:40 GMT
Connection: keep-alive
Accept-Ranges: bytes

T

Solution 2

This Nginx configuration file below allows for wildcard hostnames that dynamically route to the corresponding folder in /var/www/vhost/ while also dynamically generating the respective log files.

http://test1.wildcard.com/var/www/vhost/test1
                                                   /var/log/nginx/test1.wildcard.com-access.log                                                    /var/log/nginx/test1.wildcard.com-error.log

http://test2.wildcard.com/var/www/vhost/test2
                                                   /var/log/nginx/test2.wildcard.com-access.log                                                    /var/log/nginx/test2.wildcard.com-error.log

wildcard.conf

server {
  listen 80;
  listen [::]:80;

  #  Match everything except dot and store in $subdomain variable
  #  Matches test1.wildcard.com, test1-demo.wildcard.com
  #  Ignores sub2.test1.wildcard.com
  server_name ~^(?<subdomain>[^.]+).wildcard.com;

  root /var/www/vhost/$subdomain;

  access_log /var/log/nginx/$host-access.log;
  error_log  /var/log/nginx/$host-error.log;
}

Solution 3

This is how I've handled Virtual Hosts with Nginx:

server_name ~^(?<vhost>.*)$;
root /srv/www/$vhost;
access_log /var/log/nginx/$vhost.access.log;

I'm not sure why Wildcard Subdomains in a Parent Folder is so wrong/misleading.

Share:
77,430

Related videos on Youtube

rorygilchrist
Author by

rorygilchrist

Updated on September 17, 2022

Comments

  • rorygilchrist
    rorygilchrist almost 2 years

    I've just installed Nginx on my server and am extremely happy with the results, however I still cannot figure out how to insert wildcard virtual hosts.

    This is the [directory] structure I'd like:

    -- public_html (example.com)
    ---subdoamin 1 (x.example.com)
    ---subdomain 2 (y.example.com)
    

    As you can see it's pretty basic, however, I'd like the ability to add domains by simply adding an A record for a new subdomain, which will instantly point to the subdirectory of the same name under public_html.

    There's stuff on the web, however I haven't come across something exactly like this.

    Any help would be greatly appreciated.

    • nickgrim
      nickgrim over 13 years
      I'm not sure what you mean by "subdirectory of the same name" when your example has two different names: subdomain 1 / x.example.com - can you clarify?
    • rorygilchrist
      rorygilchrist over 13 years
      True, not very clear sorry. Lets say I have subdomain x.example.com, it's directory would be /public_html/x, however I need both example.com and www.example.com to point to /public_html/
  • rorygilchrist
    rorygilchrist over 13 years
    Doesn't work unfortunately. All subdomains just point to public_html. Here is the second server config: server{ listen 80; server_name ~^(.*)\.example\.com$ ; location / { root /var/www/public_html/$1; index index.html index.htm index.php; } location ~ \.php$ { root $1; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/public_html/$1$fastcgi_script_name; include fastcgi_params; } }
  • Alexander Azarov
    Alexander Azarov over 13 years
    "doesn't work unfortunately" gives no details unfortunately. Always look into nginx error.log for details. I've updated my answer to show you how this config works. You can see my Nginx version is 0.8.54
  • Andrew Schulman
    Andrew Schulman over 6 years
    Please explain your solution.
  • Michael Hampton
    Michael Hampton over 6 years
    This appears to be virtually identical to an existing answer. What does this add?
  • AnthumChris
    AnthumChris over 6 years
    Provides a little more specificity. Hope it helps everyone.
  • Claire Furney
    Claire Furney about 5 years
    Worked perfectly for me just now.
  • Daniel F
    Daniel F about 3 years
    Just a warning for this solution. If you have domain a.com and b.com both served from the same server, and you have a specific config file for www.b.com and b.com, where b.com is handled from a different upstream server, then abc.b.com, if not specified in the b.com config file, will be handled by a.com's upstream server.