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내가 얻어야 할 것이 아닙니다.

이것은 간단한 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;
    }
}

관련 정보