仅将client_max_body_size应用于单个URL

我想允许客户端在我们的系统上传文件,我希望他们能够上传比nginx中设置的默认主体更大的文件。

所以我在我们的网站configuration中设置了以下内容:

client_max_body_size 10m; 

到目前为止,这么好,但我真的只需要在一个单一的URL这个更大的身体尺寸; 一个用户POST到。

所以我尝试了以下configuration:

 location /api/files/ { client_max_body_size 10m; } 

但是,这根本不起作用,我在日志中得到以下错误:

2016/04/24 13:52:51 [error] 17853#0:* 1569513客户端打算发送过大的主体:1083334字节,客户端:203.0.113.7,服务器:example.com,请求:“POST / api / files HTTP / 1.1“,主机:”e​​xample.com“

所以我尝试了以下改变:

 location = /api/files { client_max_body_size 10m; } 

但是这只会导致一个不同的错误:

2016/04/24 14:25:50 [error] 19500#0:* 171 open()“/ usr / share / nginx / html / api / files”失败(2:没有这样的文件或目录),客户端:203.0 .113.7,server:example.com,请求:“POST / api / files HTTP / 1.1”,主机:“example.com”

这使得我假定location实际上是指本地文件系统上的位置,即使文档说:

根据请求URI设置configuration。

那么,如何才能将此configuration更改应用于此单个请求位置?

我认为解决scheme在于一个嵌套的位置。 所以“位置/ API /文件”应该你的PHP位置块。 您的fastcgiconfiguration应该被inheritance到“文件”位置。

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

 location ~* \.php$ { try_files $uri =404; fastcgi_pass backend; # [...] location /api/files { client_max_body_size 10m; } } 

如果你的php位置之外有位置/ api /文件,nginx会find“api / files”并记住它。 但在使用它之前,它会检查是否有一个正则expression式匹配的位置,这将是php文件请求的php位置。 然后,它会使用PHP的位置,并放弃'位置API /文件'。