gpt4 book ai didi

nginx - 我如何在 nginx 中结合重写和 client_max_body_size?

转载 作者:行者123 更新时间:2023-12-05 07:50:22 24 4
gpt4 key购买 nike

我想启用将大文件上传到单个位置,然后将该位置重写到另一个位置。似乎重写正在重置其他配置。

我的配置:

server {
listen 80;

server_name example.com;
root /var/www;
index index.php;

# this location requires large file upload
location /upload {
client_max_body_size 512M;
rewrite ^(.*)$ /index.php?request=$1 last;
}

# all other locations
location / {
rewrite ^(.*)$ /index.php?request=$1 last;
}

# pass the PHP scripts to FPM
location ~ \.php$ {
include /etc/nginx/includes/php;
}
}

如果我将 client_max_body_size 从 location 移到 server 中,那么它就可以工作了。

如果我将它同时放在 location/uploadlocation ~\.php$ 中,那么它也可以工作。但我不希望其他位置能够上传大文件。

我想我可以在 location/upload 中直接指向 PHP,但是一旦我运行重写,它无论如何都会寻找另一个位置。这是否意味着我必须为 php 脚本设置两个不同的位置?有什么办法可以让 client_max_body_size 在重写后通过其他位置保留?

最佳答案

如果您需要特定的 client_max_body_size,则需要在处理最终 URI 的 location 中设置(或继承)。在您的例子中,这是 location ~\.php$

正如您在问题中指出的那样,最简单的解决方案是处理 location/upload 中的 PHP 文件。这很容易实现,因为您已经在单独的包含文件中进行了 PHP 配置。

rewrite ... break; 或覆盖两个 fastcgi_param 指令应该适合您:

选项 1:

location /upload {
client_max_body_size 512M;

rewrite ^(.*)$ /index.php?request=$1 break;

include /etc/nginx/includes/php;
}

参见 this document了解详情。

选项 2:

location /upload {
client_max_body_size 512M;

include /etc/nginx/includes/php;

fastcgi_param QUERY_STRING request=$uri&$query_string;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}

关于nginx - 我如何在 nginx 中结合重写和 client_max_body_size?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com