我一直在使用这个服务器configuration的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; } }
首先configuration一切都像魅力,但只要我使用第二个configuration,我得到404错误。
我没有任何logging在我的error.log文件中。
如果我尝试创build一个index.html并导航到该文件一切都很好,但是当我导航到任何PHP文件时,我得到了404错误。
我想使用通配符configuration,因为我要添加和删除站点的文件夹,我不想去创build或更改nginx的服务器configuration,每次我创build或删除一个文件夹,但我一直无法做这个工作。
任何想法,我失踪? 到处search,没有发现类似的东西。
这里是nginxdebugging日志的第二个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
这就是我用curl得到的 – 我:
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
这是第一个configuration(没有通配符)的正常响应:
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
解决,感谢阿列克谢十评论。 我使用命名捕获而不是$1 :
server_name ~^(?<subdomain>.*)\.example$; root /home/user/sites/example/$subdomain/html;
这里是一个例子,如何做到这一点。
server { server_name ~^(?<subdomain>\w+)\.domainA\.com$; root /home/user/sites/example/$subdomain/html; location / { rewrite ^ https://$subdomain.domainB.com$request_uri permanent; } }