Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like all subdomains to be mapped to a particular folder and 404 if nothing exist. So if i have a wordpress install i would like nginx to see and attempt to host files from /var/log/mysite/subdomaindir/ANYTHING

How might i do that?

share|improve this question
up vote 4 down vote accepted

I am not entirely certain which scenario, of those below, you are looking for, but both are easy to accomplish. Specifying '404 if nothing exists' shouldn't be necessary as that is the default behaviour of nginx anyway (although, you can customize the error page if you want).

Scenario 1: redirect all subdomains to the SAME folder:

server {
    server_name *;
    root /var/www/mysite/subdomaindir/;

    ...your other blocks/directives


Scenario 2: redirect all subdomains, each to their own folder:

server {
    server_name ~^(?<sub>.+)\.mysite\.com$;
    root /var/www/mysite/subdomaindir/$sub;

    ...your other blocks/directives

share|improve this answer
Perfect :) (Scenario 2) – acidzombie24 Oct 31 '11 at 5:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.