NGINX PHP FastCGI 多個位置(別名)問題
第一次發帖,對這一切都很陌生-建議在這裡嘗試堆棧溢出,並閱讀了一些不同的連結,但我無法理解它。經過大量的試驗和錯誤並查看我的位置塊目前看起來像這樣 - 全域 PHP 已被刪除並包含在我的位置塊中。
第一個工作正常,經過一些更改後的第二個現在不顯示 403 Forbidden 或 404 not found 但顯示通用字元串“找不到文件”。
www.mydomain.com - 從**/var/www/html/test**提供文件 index.php
www.mydomain.com/test - 應該從 /var/www/html/test2的 index.php 提供文件,但它失敗並出現以下錯誤。
我的 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; } } }
如果有人有任何可以幫助我解決這個問題的知識,我很高興也很高興(我更相信他們而不是我自己的)來嘗試解決這個問題。
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 .