nginx 子域名的通配符 server_name 给出 index.php 404 错误

nginx 子域名的通配符 server_name 给出 index.php 404 错误

我一直在使用这个 wordpress 服务器配置,它在我的本地机器上运行良好:

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

现在我已经安装了其他 wordpress,因此我阅读了有关通配符和子域名的信息,并尝试了类似的方法:

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

使用第一个配置,一切都运行良好,但是当我使用第二个配置时,就会出现 404 错误。

我的 error.log 文件中没有记录任何内容。

如果我尝试创建一个 index.html 并导航到该文件,一切都很好,但是当我导航到任何 php 文件时,我会出现 404 错误。

我想使用通配符配置,因为我将添加和删除站点的文件夹,并且我不想每次创建或删除文件夹时都去创建或更改 nginx 的服务器配置,但我无法使其工作。

知道我遗漏了什么吗?到处搜索,没有找到类似的东西。

以下是第二种配置的请求的 nginx 调试日志:

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

下面是我使用 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

这是第一个配置的正常响应(没有通配符):

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

已解决,感谢 Alexey Ten 的评论。我使用了命名捕获,而不是$1

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

答案1

下面是如何做到这一点的一个例子。

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

相关内容