NGINX PHP FastCGI 多個位置(別名)問題

NGINX PHP FastCGI 多個位置(別名)問題

第一次發帖,對這一切都很陌生——建議在這裡嘗試堆疊溢出,並閱讀了一些連結的不同內容,但就是無法理解它。經過大量的試驗和錯誤並查看我的位置區塊目前看起來像這樣 - 全域 PHP 已被刪除並包含在我的位置區塊中。

第一個工作正常,經過一些更改後的第二個現在不顯示 403 Forbidden 或 404 not found,而是顯示通用字串“File not found”。

www.mydomain.com- 提供文件index.php/var/www/html/測試

www.mydomain.com/test- 應該從index.php提供文件 /var/www/html/test2但它失敗並出現以下錯誤。

我的Nginx訪問時錯誤日誌顯示如下:

2018/02/26 19:13:47 [error] 25229#25229: *185968 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: X.X.X.X, server: domain.co.uk, request: "GET /test/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domain.co.uk"

我一直在這裡查看有關不同 fastcgi_param SCRIPT_FILENAME 參數的各種細節,但我無法讓它們中的任何一個工作。任何建議都將非常感激,因為我花了幾個小時試圖讓它發揮作用,並設法取得一些進展,但我認為這將是完成這項工作的最終任務,這讓我感到困惑。

我已經從別名中刪除了 try_files,因為我被告知效果不佳(之前是 403),並將 $request_filename 添加到我的 fastcgi_param bu 中,但它並沒有阻止此錯誤。

location ~ ^((?!\/test).)*$ {
include /etc/nginx/mime.types;
    root /var/www/html/test;
    index index.php index.html index.htm;
    location ~ \.php$ {
        root /var/www/html/test;
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}



location ~ ^/test(.*)$ {
include /etc/nginx/mime.types;
    alias /var/www/html/test2/;
    index index.php index.html index.htm;
    location ~ \.php$ {
        alias /var/www/html/test2/;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename;
        include fastcgi_params;
    }
}

推薦我在這裡嘗試的用戶建議

Move the root /var/www/html/test; outside the first location block. nginx pitfalls
Replace alias with root in second block. according to nginx docs
remove the second alias in second block. it was redundant anyway
remove $request_filename; from fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename; same error on serverfault

我這樣做了,但這幾乎是一個後退一步,導致在訪問 /test/ 時出現 404 nginx 錯誤,並根據他們的示例進行了建議的更改,使其看起來像這樣 -

 server {
   root /var/www/html/test;
   location ~ ^((?!\/test).)*$ {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location ~ ^/test(.*)$ {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }
}

如果有人有任何知識可以幫助我解決這個問題,我很樂意(我比我自己更信任他們)嘗試並解決這個問題。

答案1

Stackoverflow 上的 Cemal 已經幫我解決了這個問題!

server {
   root /var/www/html/test;

   location /test {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location / {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }


}

404 的問題歸結為第一個位置的檔案位於 /var/www/html/test2 中,但位置 /test 帶有前綴,因此 Web 伺服器端的目錄應該是 /var/www/html/test2 。

相關內容