我有一个网站,我想创build和删除文件夹与WordPress的网站。
结构是这样的:
/wp1 /wp2 /wp3 /...
我使用nginx,我知道为了使这个工作,我必须创build多个位置块来捕获每个WordPress的网站:
location /wp1 { try_files $uri $uri/ /wp1/index.php?$args; } location /wp2 { try_files $uri $uri/ /wp2/index.php?$args; } location /wp3 { try_files $uri $uri/ /wp3/index.php?$args; } ...
该coonfiguration作品完美,但很难保持,所以我试图与位置的正则expression式,以便只使用一个位置块的所有网站,所以我可以删除和创build文件夹,而不必担心nginx设置:
location ~ ^/wp(?<staging>\d+) { try_files $uri $uri/ /wp$staging/index.php?$args; }
但是这不起作用。 任何想法,我在想什么?
这是我的完整configuration文件:
server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /home/city/sites/staging1/html; index index.php; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location / { try_files $uri $uri/ /index.php?$args; } location ~ ^/stg(?<staging>\d+) { try_files $uri $uri/ /stg$staging/index.php?$args; } location ~ \.php$ { include fastcgi.conf; fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php7.0-fpm.city.sock; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; log_not_found off; } }
这是curl -I http://ipaddress/wp2的输出curl -I http://ipaddress/wp2
HTTP/1.1 301 Moved Permanently Server: nginx Date: Tue, 17 Jan 2017 03:45:43 GMT Content-Type: text/html Content-Length: 178 Location: http://ipaddress/wp2/ Connection: keep-alive
这是curl -I http://ipaddress/wp2/的输出curl -I http://ipaddress/wp2/
HTTP/1.1 200 OK Server: nginx Date: Tue, 17 Jan 2017 03:45:49 GMT Content-Type: application/octet-stream Content-Length: 418 Last-Modified: Wed, 25 Sep 2013 00:18:11 GMT Connection: keep-alive ETag: "52422bc3-1a2" Accept-Ranges: bytes
但是这下载了index.php wordpress文件,内容types是application/octet-stream ,这不是我应该得到的。
这是一个预期的curl输出,没有正则expression式位置块,一个简单的WordPress的网站:
HTTP/1.1 200 OK Server: nginx Date: Tue, 17 Jan 2017 04:30:30 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Link: <http://ipaddress/wp2/wp-json/>; rel="https://api.w.org/" Link: <http://ipaddress/wp2/>; rel=shortlink
在nginx日志文件中,我可以看到正则expression式的位置正在被使用,但我不明白是什么问题,为什么我得到这个响应:
[debug] 24359#24359: *1330 test location: "/" [debug] 24359#24359: *1330 test location: "favicon.ico" [debug] 24359#24359: *1330 test location: "robots.txt" [debug] 24359#24359: *1330 test location: ~ "^/wp(?<staging>\d+)" [debug] 24359#24359: *1330 http regex set $staging to "2" [debug] 24359#24359: *1330 using configuration "^/wp(?<staging>\d+)" [debug] 24359#24359: *1330 http cl:-1 max:1048576 [debug] 24359#24359: *1330 rewrite phase: 3 [debug] 24359#24359: *1330 post rewrite phase: 4 [debug] 24359#24359: *1330 generic phase: 5 [debug] 24359#24359: *1330 generic phase: 6 [debug] 24359#24359: *1330 generic phase: 7 [debug] 24359#24359: *1330 access phase: 8 [debug] 24359#24359: *1330 access phase: 9 [debug] 24359#24359: *1330 access phase: 10 [debug] 24359#24359: *1330 post access phase: 11 [debug] 24359#24359: *1330 try files phase: 12 [debug] 24359#24359: *1330 http script var: "/wp2" [debug] 24359#24359: *1330 trying to use file: "/wp2" "/home/city/sites/staging1/html/wp2" [debug] 24359#24359: *1330 http script var: "/wp2" [debug] 24359#24359: *1330 trying to use dir: "/wp2" "/home/city/sites/staging1/html/wp2" [debug] 24359#24359: *1330 try file uri: "/wp2" [debug] 24359#24359: *1330 content phase: 13 [debug] 24359#24359: *1330 content phase: 14 [debug] 24359#24359: *1330 content phase: 15 [debug] 24359#24359: *1330 content phase: 16 [debug] 24359#24359: *1330 content phase: 17 [debug] 24359#24359: *1330 http filename: "/home/city/sites/staging1/html/wp2"
问题似乎是你的PHP没有被执行,它只是被下载。
根据您的评论,您应该在自己的答案,对这个答案的评论或编辑您的问题中描述解决scheme。 我认为对于信誉度较低的用户来说,这一点有限。
问题是,正则expression式位置不使用:
location ~ \.php$ { include fastcgi.conf; fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php7.0-fpm.city.sock; }
所以发生了什么事是PHP文件没有执行。
为了解决这个问题,刚刚在有问题的正则expression式位置添加了一个location ~ \.php$ :
location ~ ^/stg_(?<staging>\w+) { try_files $uri $uri/ /stg_$staging/index.php?$args; location ~ \.php$ { include fastcgi.conf; fastcgi_intercept_errors on; fastcgi_pass unix:/run/php/php7.0-fpm.city.sock; } }