我有一個帶有兩個容器的 laravel 應用程式的 kubernetes 部署:
NGINX 容器接收請求并立即回傳容器上的靜態檔案(影像、javascript、css..),或者,如果請求的檔案不存在,則將請求代理到 PHP 容器
運行 Laravel 的 PHP 容器
它與以下 nginx 配置完美配合:
server {
listen 80;
root /var/www/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|ttf|woff|woff2)$ {
expires 2d;
add_header Cache-Control "public, no-transform";
}
}
問題來了:我需要回傳一些應該由 laravel 處理的加密檔案(處理授權、認證和解密)。使用以下端點請求這些檔案:
example.com/files/decrypt/path/to/file.jpg?token=tokentovalidaterequest
這樣的請求會生成 nginx 錯誤和 404 回應(從 nginx 日志中,我用 $path 替換了請求的路徑):
2021/10/28 08:29:22 [error] 24#24: *1 open() "/var/www/public/files/decrypt/$path" failed (2: No such file or directory), client: 10.244.0.97, server: , request: "GET /files/decrypt/files/decrypt/$path?hash=$hash HTTP/1.1", host: "example.com"
10.244.0.97 - - [28/Oct/2021:08:29:22 0000] "GET /files/decrypt/$path?hash=$hash HTTP/1.1" 404 6622 "https://example.com" "user-agent" "ip"
由于以下原因,請求實際上由 php 處理:
error_page 404 /index.php;
但它丟失了查詢字串引數,我不希望我的 nginx 日志充滿虛假錯誤。
有沒有辦法告訴 nginx“如果位置以 開頭/files,則將請求直接發送到 php,而不檢查檔案系統上是否存在該檔案”?
我嘗試添加:
location /files {
try_files /index.php?$query_string;
}
在location /塊之前,但我得到了一個 nginx 配置錯誤
實作這一目標的正確方法是什么?
uj5u.com熱心網友回復:
該try_files陳述句至少需要兩個引數,請參閱此檔案。您可以添加一個假檔案名作為第一個引數。
例如:
try_files nonexistent /index.php?$query_string;
或者,rewrite陳述句也可以作業,并注意rewrite將自動附加查詢字串,請參閱此檔案。
例如:
rewrite ^ /index.php last;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339931.html
