Nginx 하위 디렉터리 try_files 와일드카드는 항상 실패합니다.

Nginx 하위 디렉터리 try_files 와일드카드는 항상 실패합니다.

Nginx 1.10.3 Ubuntu, 표준 적절한 설치. 블록 index index.php;밖에 위치 server.

나는 필요하다:

  • http://example.com/test/1가리키는 /var/www/example.com/test/1
  • http://example.com/test/2가리키는 /var/www/example.com/test/2

..등등.

너무 많은 테스트를 생성할 것이기 때문에 에 대한 와일드카드가 필요합니다 try_files. 현재 나는 와일드카드 없이 작업을 수행하고 있습니다.

server {
    server_name example.com;
    root   /var/www/example.com;
    location /test/1/ {
        try_files $uri $uri/ /test/1/index.php?$args;
    }
    location /test/2/ {
        try_files $uri $uri/ /test/2/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

많은 권장 사항 중 어느 것도 작동하지 않습니다.

일반 PHP가 잘 실행됩니다. WordPress & Laravel은 "파일을 찾을 수 없습니다"를 표시했습니다.

server {
    server_name example.com;
    location ~ ^/test/(?<content>.+)$ {
        root   /var/www/example.com/test/$content;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        ...
}

파일을 찾을 수 없습니다:

server {
    server_name example.com;
    location ~ ^/test/(?<content>[^/]+) {
        root   /var/www/example.com/test/$content;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        ...
}

아래의 모든 시도에서 PHP를 실행하는 대신 PHP 파일을 다운로드합니다.

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /(?<content>[^/]+) {
        try_files $uri $uri/ /$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /(.*)/ {
        try_files $uri $uri/ /$1/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /test/(?<content>[^/]+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ /test/(?<content>.+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
    location ~ \.php$ {
        ...
}

가능하다면 정답에 대해 10달러를 기꺼이 기부하겠습니다.

답변1

정규식 location블록은 순서대로 평가되므로 .php블록은 블록 앞에 배치되어야 합니다 /test/.... 그렇지 않으면 .php아래의 파일이 /test/실행되는 대신 다운로드됩니다. 보다이 문서자세한 내용은.

가장 좋은 버전은 마지막에서 두 번째였습니다. 정규식은 접두사 뒤에 오는 경로 요소만 추출합니다 /test/.

블록 을 반대로 하면 됩니다 location. 예를 들어:

server {
    server_name example.com;
    root   /var/www/example.com;
    location ~ \.php$ {
        ...
    }
    location ~ /test/(?<content>[^/]+) {
        try_files $uri $uri/ /test/$content/index.php?$args;
    }
}

관련 정보