curinga nginx server_name para subdomínio fornece erro index.php 404

1

Eu tenho usado essa configuração de servidor para o wordpress, que funciona muito bem na minha máquina local:

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;
    }
}

Agora eu tenho outras instalações do wordpress, então eu li sobre curingas e subdomínios e tentei algo assim:

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;
    }
}

Com a primeira configuração, tudo funciona como um charme, mas assim que eu uso a segunda configuração, recebo um erro 404.

Eu não tenho nada registrado no meu arquivo error.log.

Se eu tentar criar um index.html e navegar para esse arquivo, tudo estará bem, mas quando eu navego para qualquer arquivo php, recebo o erro 404.

Eu gostaria de usar a configuração de curingas como eu vou estar adicionando e removendo pastas para sites e eu não quero ir e criar ou alterar configurações de servidores para nginx toda vez que eu criar ou excluir uma pasta, mas eu não tenho Não consegui fazer isso funcionar.

Alguma ideia do que me falta? Pesquisei em todos os lugares e ainda não encontrei algo semelhante.

Aqui está o log de depuração do nginx para a solicitação com a segunda configuração:

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

E aqui está o que eu recebo com o 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

Esta é a resposta normal com a primeira configuração (sem curingas):

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

Resolvido, graças a Alexey Dez comentário. Eu usei captura de nome em vez de $1 :

server_name ~^(?<subdomain>.*)\.example$;
root /home/user/sites/example/$subdomain/html;
    
por archemiro 28.10.2014 / 16:12

1 resposta

0

Aqui está um exemplo de como fazer isso.

link

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

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

    location / {
            rewrite ^ https://$subdomain.domainB.com$request_uri permanent;
    }
}
    
por 29.10.2014 / 13:35