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

相關內容