如何設定 nginx 和 fastcgi 在子資料夾中運行 Laravel?

如何設定 nginx 和 fastcgi 在子資料夾中運行 Laravel?

我正在單一網域的子資料夾下設定多個 Laravel 應用程式(社交網路應用程序,需要 https,單一憑證)。

無法弄清楚如何編寫 nginx 設定來將 HTTP 請求對應到/app1/Laravel 安裝/var/www/other_folder/public/

這是配置測試的最新迭代。省略調試日誌記錄。 Laravel 根位置 /app1/ 有效,但路由對映 ( /app1/api/method/) 無效。請幫忙,如何調試 nginx 如何逐步處理請求(調試日誌不是那麼解釋性的),或者給我一個提示,如何將子資料夾映射/app1/...index.phpLaravel 的子資料夾。

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 文檔,根指令也有上下文「location」:

語法:根路徑;

上下文: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
}

據我了解,別名指令將一個 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;
}

其次, $query_string 與 $args 相同,唯一的區別是它是只讀。

就像我說的,這還沒有經過測試,但如果它有效的話那就太好了。如果沒有,我還有另一個想法。

相關內容