1

I've been using this server configuration for wordpress which works great on my local machine:

server {
    server_name example.example

    root /home/user/sites/example/example/html;

    index index.php index.html index.htm;

    access_log off;
    error_log /home/user/log/error.log;

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
            try_files $uri =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;
    }
}

Now I've other wordpress installations, so I read about wildcards and subdomains and tried something like this:

server {
    server_name ~^(.*)\.example;

    root /home/user/sites/example/$1/html;

    index index.php index.html index.htm;

    access_log off;
    error_log /home/user/log/$1.error.log;

    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
            try_files $uri =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;
    }
}

With the first configuration everything works like a charm, but as soon as I use the second configuration I get 404 error.

I do not have anything logged in my error.log file.

If I try create an index.html and navigate to that file everything is fine, but when I navigate to any php file I get the 404 error.

I would like to use the wildcard configuration as I'm going to be adding and removing folders for sites and I dont want to go and create or change servers configurations for nginx everytime I create or delete a folder, but I haven't been able to make this work.

Any ideas on what I'm missing? Searched everywhere and havent found something similar.

Here is the debug log of nginx for the request with the second configuration:

2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Host: nugomo.nugomo"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Accept-Language: en-us,en;q=0.5"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Accept-Encoding: gzip, deflate"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Connection: keep-alive"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header: "Cache-Control: max-age=0"
2014/10/28 15:54:54 [debug] 5680#0: *1 http header done
2014/10/28 15:54:54 [debug] 5680#0: *1 event timer del: 13: 1414529754168
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 rewrite phase: 1
2014/10/28 15:54:54 [debug] 5680#0: *1 test location: "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 test location: ~ "\.php$"
2014/10/28 15:54:54 [debug] 5680#0: *1 using configuration "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 http cl:-1 max:1048576
2014/10/28 15:54:54 [debug] 5680#0: *1 rewrite phase: 3
2014/10/28 15:54:54 [debug] 5680#0: *1 post rewrite phase: 4
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 5
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 6
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 7
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 8
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 9
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 10
2014/10/28 15:54:54 [debug] 5680#0: *1 post access phase: 11
2014/10/28 15:54:54 [debug] 5680#0: *1 try files phase: 12
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/home/mjrofra/vps/nugomo/"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script capture: "nugomo"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/html"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script var: "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 trying to use file: "/" "/home/mjrofra/vps/nugomo/nugomo/html/"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script var: "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 trying to use dir: "/" "/home/mjrofra/vps/nugomo/nugomo/html/"
2014/10/28 15:54:54 [debug] 5680#0: *1 try file uri: "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 content phase: 13
2014/10/28 15:54:54 [debug] 5680#0: *1 content phase: 14
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/home/mjrofra/vps/nugomo/"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script capture: "nugomo"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/html"
2014/10/28 15:54:54 [debug] 5680#0: *1 open index "/home/mjrofra/vps/nugomo/nugomo/html/index.php"
2014/10/28 15:54:54 [debug] 5680#0: *1 internal redirect: "/index.php?"
2014/10/28 15:54:54 [debug] 5680#0: *1 rewrite phase: 1
2014/10/28 15:54:54 [debug] 5680#0: *1 test location: "/"
2014/10/28 15:54:54 [debug] 5680#0: *1 test location: ~ "\.php$"
2014/10/28 15:54:54 [debug] 5680#0: *1 using configuration "\.php$"
2014/10/28 15:54:54 [debug] 5680#0: *1 http cl:-1 max:1048576
2014/10/28 15:54:54 [debug] 5680#0: *1 rewrite phase: 3
2014/10/28 15:54:54 [debug] 5680#0: *1 post rewrite phase: 4
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 5
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 6
2014/10/28 15:54:54 [debug] 5680#0: *1 generic phase: 7
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 8
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 9
2014/10/28 15:54:54 [debug] 5680#0: *1 access phase: 10
2014/10/28 15:54:54 [debug] 5680#0: *1 post access phase: 11
2014/10/28 15:54:54 [debug] 5680#0: *1 try files phase: 12
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/home/mjrofra/vps/nugomo/"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script capture: ""
2014/10/28 15:54:54 [debug] 5680#0: *1 http script copy: "/html"
2014/10/28 15:54:54 [debug] 5680#0: *1 http script var: "/index.php"
2014/10/28 15:54:54 [debug] 5680#0: *1 trying to use file: "/index.php" "/home/mjrofra/vps/nugomo//html/index.php"
2014/10/28 15:54:54 [debug] 5680#0: *1 trying to use file: "=404" "/home/mjrofra/vps/nugomo//html=404"
2014/10/28 15:54:54 [debug] 5680#0: *1 http finalize request: 404, "/index.php?" a:1, c:2
2014/10/28 15:54:54 [debug] 5680#0: *1 http special response: 404, "/index.php?"
2014/10/28 15:54:54 [debug] 5680#0: *1 http set discard body
2014/10/28 15:54:54 [debug] 5680#0: *1 xslt filter header
2014/10/28 15:54:54 [debug] 5680#0: *1 HTTP/1.1 404 Not Found
Server: nginx/1.4.4
Date: Tue, 28 Oct 2014 20:54:54 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip

2014/10/28 15:54:54 [debug] 5680#0: *1 write new buf t:1 f:0 000000000143D560, pos 000000000143D560, size: 185 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 http write filter: l:0 f:0 s:185
2014/10/28 15:54:54 [debug] 5680#0: *1 http output filter "/index.php?"
2014/10/28 15:54:54 [debug] 5680#0: *1 http copy filter: "/index.php?"
2014/10/28 15:54:54 [debug] 5680#0: *1 image filter
2014/10/28 15:54:54 [debug] 5680#0: *1 xslt filter body
2014/10/28 15:54:54 [debug] 5680#0: *1 http postpone filter "/index.php?" 000000000143D630
2014/10/28 15:54:54 [debug] 5680#0: *1 http gzip filter
2014/10/28 15:54:54 [debug] 5680#0: *1 malloc: 0000000001433CF0:12288
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip alloc: n:1 s:5936 a:8192 p:0000000001433CF0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip alloc: n:512 s:2 a:1024 p:0000000001435CF0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip alloc: n:512 s:2 a:1024 p:00000000014360F0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip alloc: n:512 s:2 a:1024 p:00000000014364F0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip alloc: n:256 s:4 a:1024 p:00000000014368F0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in: 00000000014331D8
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in_buf:00000000014330C0 ni:00000000006C9FE0 ai:116
2014/10/28 15:54:54 [debug] 5680#0: *1 malloc: 00000000014B0380:4096
2014/10/28 15:54:54 [debug] 5680#0: *1 deflate in: ni:00000000006C9FE0 no:00000000014B0380 ai:116 ao:4096 fl:0 redo:0
2014/10/28 15:54:54 [debug] 5680#0: *1 deflate out: ni:00000000006CA054 no:00000000014B0380 ai:0 ao:4096 rc:0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in_buf:00000000014330C0 pos:00000000006C9FE0
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in: 00000000014331E8
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in_buf:0000000001433110 ni:00000000006CA720 ai:52
2014/10/28 15:54:54 [debug] 5680#0: *1 deflate in: ni:00000000006CA720 no:00000000014B0380 ai:52 ao:4096 fl:4 redo:0
2014/10/28 15:54:54 [debug] 5680#0: *1 deflate out: ni:00000000006CA754 no:00000000014B03F1 ai:0 ao:3983 rc:1
2014/10/28 15:54:54 [debug] 5680#0: *1 gzip in_buf:0000000001433110 pos:00000000006CA720
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 0000000001433CF0
2014/10/28 15:54:54 [debug] 5680#0: *1 http chunk: 10
2014/10/28 15:54:54 [debug] 5680#0: *1 http chunk: 121
2014/10/28 15:54:54 [debug] 5680#0: *1 write old buf t:1 f:0 000000000143D560, pos 000000000143D560, size: 185 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 write new buf t:1 f:0 0000000001433348, pos 0000000001433348, size: 4 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 write new buf t:0 f:0 0000000000000000, pos 00000000006CD800, size: 10 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 write new buf t:1 f:0 00000000014B0380, pos 00000000014B0380, size: 121 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 write new buf t:0 f:0 0000000000000000, pos 00000000004A4338, size: 7 file: 0, size: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 http write filter: l:1 f:1 s:327
2014/10/28 15:54:54 [debug] 5680#0: *1 http write filter limit 0
2014/10/28 15:54:54 [debug] 5680#0: *1 writev: 327
2014/10/28 15:54:54 [debug] 5680#0: *1 http write filter 0000000000000000
2014/10/28 15:54:54 [debug] 5680#0: *1 http copy filter: 0 "/index.php?"
2014/10/28 15:54:54 [debug] 5680#0: *1 http finalize request: 0, "/index.php?" a:1, c:2
2014/10/28 15:54:54 [debug] 5680#0: *1 http request count:2 blk:0
2014/10/28 15:54:54 [debug] 5680#0: *1 http finalize request: -4, "/index.php?" a:1, c:1
2014/10/28 15:54:54 [debug] 5680#0: *1 set http keepalive handler
2014/10/28 15:54:54 [debug] 5680#0: *1 http close request
2014/10/28 15:54:54 [debug] 5680#0: *1 http log handler
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 00000000014B0380
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 0000000000000000
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 000000000143C650, unused: 0
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 0000000001432CE0, unused: 2272
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 000000000143C240
2014/10/28 15:54:54 [debug] 5680#0: *1 hc free: 0000000000000000 0
2014/10/28 15:54:54 [debug] 5680#0: *1 hc busy: 0000000000000000 0
2014/10/28 15:54:54 [debug] 5680#0: *1 tcp_nodelay
2014/10/28 15:54:54 [debug] 5680#0: *1 reusable connection: 1
2014/10/28 15:54:54 [debug] 5680#0: *1 event timer add: 13: 65000:1414529759169
2014/10/28 15:54:54 [debug] 5680#0: *1 post event 0000000001489430
2014/10/28 15:54:54 [debug] 5680#0: *1 delete posted event 0000000001489430
2014/10/28 15:54:54 [debug] 5680#0: *1 http keepalive handler
2014/10/28 15:54:54 [debug] 5680#0: *1 malloc: 000000000143C240:1024
2014/10/28 15:54:54 [debug] 5680#0: *1 recv: fd:13 -1 of 1024
2014/10/28 15:54:54 [debug] 5680#0: *1 recv() not ready (11: Resource temporarily unavailable)
2014/10/28 15:54:54 [debug] 5680#0: *1 free: 000000000143C240

And here is what I get with curl -I:

HTTP/1.1 404 Not Found
Server: nginx/1.4.4
Date: Tue, 28 Oct 2014 21:12:21 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

This is the normal response with the first configuration (without wildcards):

HTTP/1.1 200 OK
Server: nginx/1.4.4
Date: Tue, 28 Oct 2014 21:11:51 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.6-1
X-Pingback: http://nugomo.nugomo/xmlrpc.php

Solved, thanks to Alexey Ten comment. I used named capture instead of $1:

server_name ~^(?<subdomain>.*)\.example$;
root /home/user/sites/example/$subdomain/html;
MauricioRobayo
  • 152
  • 1
  • 14

1 Answers1

0

Here is an example how to do that.

https://stackoverflow.com/questions/9578628/redirecting-a-subdomain-with-a-regular-expression-in-nginx

server {
    server_name     ~^(?<subdomain>\w+)\.domainA\.com$;

    root /home/user/sites/example/$subdomain/html;

    location / {
            rewrite ^ https://$subdomain.domainB.com$request_uri permanent;
    }
}
René Höhle
  • 1,438
  • 3
  • 17
  • 26
  • Thank you Stony. I think the regexp you are using wont match something like `test-subdomain.example.com`. Also, I don't have the need of the rewrite rule as I'm not trying to go from domainA to domainB. Already solved using named capture as Alexey Ten said in his comment. Anyway thank you for your suggestion. – MauricioRobayo Oct 29 '14 at 12:53
  • This is the stackoverflow example. You can customize the regex for your needs. – René Höhle Oct 29 '14 at 12:54