
我正在尋找解決方案這個問題。我理解該問題中的設定不起作用的原因,但我嘗試找到一個可以使其工作的解決方案。
這個想法是只允許在某些 URL 上上傳大檔案。我可以location
為此使用一個塊,但問題是:我有一個 php frontcontroller 模式:
location ~ \.php {
# ...
fastcgi_pass unix:/tmp/php5-fpm.sock;
}
我的總配置如下:
# ...
http {
# ...
client_max_body_size 512K;
server {
server_name example.com;
root /var/www/example.com/public;
location / {
try_files $uri /index.php?$query_string;
}
location /admin/upload {
client_max_body_size 256M;
}
location ~ \.php {
# ...
fastcgi_pass unix:/tmp/php5-fpm.sock;
}
}
}
據我了解,只會套用一個位置塊。因此,如果我的預設請求大小為 512K,則永遠不會套用 256M,因為所有請求都透過 frontcontroller 模式進行比對~ \.php
。
在這種情況下我是否正確admin/upload
?
答案1
如果/admin/upload
路徑是虛擬的,則可以如下使其工作:
location / {
try_files $uri /index.php?$args;
}
location /admin/upload {
client_max_body_size 256M;
include inc/php.conf;
rewrite ^(.*)$ /index.php?$args break;
}
location ~ \.php$ {
include inc/php.conf;
}
也不是最漂亮的,但很有效。
答案2
定義兩個php位置?
location ~ ^/admin/upload/.+\.php$
{
client_max_body_size 256M;
include /etc/nginx/conf.d/php-fpm.conf;
}
location ~ \.php
{
include /etc/nginx/conf.d/php-fpm.conf;
}
也許不是最漂亮的...但應該是實用的..
答案3
使用多個位置是可能的,但有點棘手。
如果您使用try_files
or ,rewrite
如上所述,那麼client_max_body_size
將會被設定為client_max_body_size
更高上下文的 ,而不是您期望的位置區塊的值。
將 PHP FastCGI 配置移至可包含的檔案中,例如php-conf.conf
.
然後使用這樣的配置:
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$query_string;
}
location ~ ^/admin/upload$ {
client_max_body_size 4m;
include /etc/nginx/php-conf.conf;
fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
}
location ~ ^/index\.php(/|$) {
include /etc/nginx/php-conf.conf;
internal;
}
請注意,如果設定了不同的腳本名稱,則需要覆蓋SCRIPT_FILENAME
才能使用。index.php
發生這種情況的原因是fastcgi_split_path_info ^(.+\.php)(/.*)$;
.