Nginx サブディレクトリの try_files ワイルドカードは常に失敗する

Nginx サブディレクトリの try_files ワイルドカードは常に失敗する

Nginx 1.10.3 Ubuntu、標準の apt インストール。ブロック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/実行されずにダウンロードされます。このドキュメント詳細については。

ベストバージョンは最後から 2 番目でした。正規表現は/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;
    }
}

関連情報