サブフォルダーで Laravel を実行するために nginx と fastcgi を設定するにはどうすればいいですか?

サブフォルダーで Laravel を実行するために nginx と fastcgi を設定するにはどうすればいいですか?

単一ドメインのサブフォルダーの下に複数の Laravel アプリを設定しています (ソーシャル ネットワーク アプリ、https が必要、単一証明書)。

/app1/HTTPリクエストをLaravelインストールにマップするためのnginx設定の書き方がわかりません/var/www/other_folder/public/

これが最新の構成テストの繰り返しです。デバッグ ログは省略されています。Laravel のルートの場所 /app1/ は機能しますが、ルート ( /app1/api/method/) のマッピングは機能しません。nginx がリクエストを段階的に処理する方法をデバッグする方法 (デバッグ ログでは説明が不十分)、または のサブフォルダーをLaravel/app1/...の にマッピングする方法を教えてくださいindex.php

server {
    listen      80;
    server_name  apps.mydomain.com;
    root    /var/www/apps.mydomain.com/docs;

    location /  {
        index   index.html index.php;
    }

    location /app1    {
        alias   /var/www/other_folder/public;
        index   index.php   index.html;
        try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
    }

    location ~ /app1/.+\.php$ {
        rewrite ^/app1/(.*)$  /$1  break;
        include /etc/nginx/fastcgi.conf;
        fastcgi_param    SCRIPT_FILENAME    /var/www/other_folder/public$fastcgi_script_name;

        fastcgi_index  index.php;
        fastcgi_pass php;

        #   Database access parameters
        fastcgi_param   DB_HOST "localhost";
        fastcgi_param   DB_USER "apps";
        fastcgi_param   DB_PASS "xxxxxxxx";
        fastcgi_param   DB_NAME "app1";
    }

    # Other locations skipped
    include /etc/nginx/global/php.conf; # other php scripts
}

答え1

場所のルートを変更する必要があると思います。Nginx のドキュメントによると、ルート ディレクティブにはコンテキスト「場所」もあります。

構文: ルートパス;

コンテキスト: http、サーバー、場所、場所にある場合

したがって、次のようなことができるはずです。

server {
    listen      80;
    server_name  apps.mydomain.com;
    root    /var/www/apps.mydomain.com/docs;

    location /  {
        index   index.html index.php;
    }

    location /app1    {
        root   /var/www/other_folder/public;
        rewrite ^/app1/(.*)$  /$1  break;
        index   index.php   index.html;
        try_files   $uri  $uri/ /index.php$is_args$args /index.php?$query_string;
    }

    location ~ /app1/.+\.php$ {
        root   /var/www/other_folder/public;
        rewrite ^/app1/(.*)$  /$1  break;
        include /etc/nginx/fastcgi.conf;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;

        fastcgi_index  index.php;
        fastcgi_pass php;

        #   Database access parameters
        fastcgi_param   DB_HOST "localhost";
        fastcgi_param   DB_USER "apps";
        fastcgi_param   DB_PASS "xxxxxxxx";
        fastcgi_param   DB_NAME "app1";
    }

    # Other locations skipped
    include /etc/nginx/global/php.conf; # other php scripts
}

私の理解する限りでは、alias ディレクティブは URL を別の URL に再マップして処理を続行しますが、ソース ファイルのディレクトリは定義しません。PHP の場所の書き換えがまだ必要かどうかはわかりません。

答え2

このコードはテストされていません。しかし、 /app1/ では動作しますが、 /app1/api/method/ では動作しないと言っています。問題は次の部分にあると思います:

location /app1    {
    alias   /var/www/other_folder/public;
    index   index.php   index.html;
    try_files   $uri  $uri/ /app1/index.php$is_args$args /app1/index.php?$query_string;
}

つまり、これは基本的に を試してurl、一致しない場合はディレクトリ として一致させようurl/とし、ファイルとディレクトリが存在しない場合は として一致させようとし/app1/index.php?someargs、存在しない場合は として一致させます。/app1/index.php?$query_string

初めに、エイリアスとtry_files連携して動作しないため、代わりに root を使用できます。/app1 の場所ブロックを次のように変更して、問題が解決するかどうかを確認してください。

location /app1    {
    root   /var/www/other_folder/public;
    index   index.php   index.html;
    try_files $uri $uri/ /index.php$is_args$args;
}

2番目に、$query_stringは$argsと同じですが、唯一の違いは読み取り専用。

前に言ったように、これはテストされていませんが、うまくいけばいいのですが。うまくいかなかったとしても、別のアイデアがあります。

関連情報