我正在構建一個運行 NginX / PHP 的站點,并希望所有訪問都由 /private/routes.php 處理。但我還想為公共目錄(和子目錄)中的 css/js-files 和奇怪的 php 檔案添加一些例外。
檔案夾/檔案結構:
/var/www/domain.com
/private/routes.php
/public/test.php
/public/css/main.css
/public/js/main.js
我當前的 NginX 配置:
server {
listen 80;
listen [::]:80;
server_name domain.com;
root /var/www/domain.com/public;
#index index.php;
#allow existing css files
location /css/ {
try_files $uri =404;
}
#allow my defined php files
location /test.php {
try_files $uri =404;
}
#route everything else to my catch-all file
location / {
root /var/www/domain.com;
try_files /private/nada.php /private/routes.php;
#must add php here because of redefined root
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
#handle allowed php files
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
上面的配置有效,但有一些問題:
- 我需要重新定義根來運行捕獲所有檔案(/private/routes.php)。
- 因為#1 我需要在位置內添加 .php,所以我最終得到了 2 個 php-location-configs。
- 出于某種原因,如果我從“try_files”中洗掉“/private/nada.php”,它就不起作用。
- /public 中不存在的 .php 檔案回傳 404 而不是被包羅萬象的捕獲。
我可以將根設定為“/var/www/domain”,但這似乎有點不必要,因為只有包羅萬象才會使用其他東西。
有什么好的方法可以清理一下嗎?
uj5u.com熱心網友回復:
你的配置真的是一團糟。我不會解釋每個錯誤,否則會使答案延長 10 倍。我花了一些時間才弄清楚它是如何部分可行的。最有趣的是,您location /test.php { ... }并沒有真正用于處理/test.php請求(或者您已經獲得了test.php源代碼作為回應),但是如果您洗掉它,該請求將由嵌套的 PHP 處理程式而不是根處理程式處理.
由于您想要實作的配置不屬于通常的用例,因此您不應fastcgi-php.conf使用在大多數情況下可用的某種預先撰寫的 PHP-FPM 處理程式。它不是 nginx 團隊提供的默認 nginx 包的一部分,而是由一些發行版與 nginx 打包在一起,尤其是基于 Debian 的發行版。由于它已經包含try_files指令,我們需要使用 customtry_files代替,我們不能使用該檔案,否則 nginx 會抱怨重復try_files使用。
這是一個應該作業的配置:
server {
...
root /var/www/domain.com/public;
# process an existing non-PHP file as a static one, fallback otherwise
location / {
try_files $uri @fallback;
}
# process an existing PHP file through PHP-FPM, fallback otherwise
location ~ \.php$ {
try_files $uri @fallback;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
# fallback to /private/routes.php
location @fallback {
include fastcgi_params;
# we don't need a "root" directive here
# instead we can specify the fallback filename directly
fastcgi_param SCRIPT_FILENAME /var/www/domain.com/private/routes.php;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
這種型別的 PHP-FPM 用法與通常的用法略有不同,因為fastcgi_params檔案中定義了幾個 FastCGI 變數,這些變數取決于rootnginx 的實際使用或實際$uri變數值:
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
我從未見過任何 PHP 腳本以某種方式依賴這些,通常的做法是依賴REQUEST_URI變數值,但我認為值得注意到這種差異。但是,如果需要,可以重新定義這些變數(在這種情況下,這應該在該include fastcgi_params;行之后完成)。例如,以下將完全模擬 PHP 處理程式的通常行為:
location @fallback {
include fastcgi_params;
fastcgi_param SCRIPT_NAME /private/routes.php;
fastcgi_param DOCUMENT_URI /private/routes.php;
fastcgi_param DOCUMENT_ROOT /var/www/domain.com;
fastcgi_param SCRIPT_FILENAME /var/www/domain.com/private/routes.php;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
或者您可以定義一個不同的根,重寫一個 URI 并使用常用的:
location @fallback {
root /var/www/domain.com;
rewrite ^ /private/routes.php break;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
但是,正如已經說過的,很可能不需要它。
如果在public目錄下您有包含index.php檔案的子目錄,并且您希望這些檔案可用而不用指定檔案名的完整路徑(例如,domain.com/subdir/而不是domain.com/subdir/index.php),請將根位置更改為以下位置:
location / {
index index.php;
try_files $uri $uri/ @fallback;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474904.html
下一篇:頻道Djangonginx502
