nginx 位置正则表达式变量不适用于 try_file

nginx 位置正则表达式变量不适用于 try_file

我有一个网站,我想在其中创建和删除带有暂存 wordpress 网站的文件夹。

结构如下:

/wp1
/wp2
/wp3
/...

我正在使用 nginx,并且我知道为了完成这项工作,我必须创建多个位置块来捕获每个 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;
}
...

该配置工作完美,但很难维护,因此我尝试使用位置的正则表达式,以便对所有站点仅使用一个位置块,这样我就可以删除和创建文件夹,而不必担心 nginx 设置:

location ~ ^/wp(?<staging>\d+) {
    try_files $uri $uri/ /wp$staging/index.php?$args;
}

但这不起作用。有什么想法可以告诉我我遗漏了什么吗?

这是我的完整配置文件:

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

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/

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 文件,其内容类型application/octet-stream不是我应该得到的。

这是没有正则表达式位置块的预期 curl 输出,一个简单的 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 日志文件中,我可以看到正则表达式位置正在被使用,但我不明白问题是什么以及为什么我会得到这样的响应:

[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"

答案1

问题似乎是您的 PHP 没有被执行,而只是被下载。

根据您的评论,您应该描述解决方案,无论是在您自己的答案中,还是对此答案的评论中,或者编辑您的问题。我认为对于声誉值较低的用户来说,事情有点受限。

答案2

问题是正则表达式位置没有使用:

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.city.sock;
}

所以发生的事情是 php 文件没有执行。

为了解决这个问题,只需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;
    }
}

相关内容