每个位置块的Nginx的client_max_body_size(用php的frontcontroller模式)

我正在寻找这个问题的解决scheme。 我理解为什么在这个问题的设置不起作用的原因,但我试图find一个解决scheme,我可以得到它的工作。

这个想法是只允许在某些URL上进行大file upload。 我可以使用这个location块,但问题是:我有一个PHP的前端控制器模式:

 location ~ \.php { # ... fastcgi_pass unix:/tmp/php5-fpm.sock; } 

我的总configuration看起来像:

 # ... 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永远不会被应用,因为所有的请求都通过前台控制器模式~ \.php匹配。

在这种情况下我是否正确,如果是这样,可以configuration什么,除非上传admin/upload否则访问者不能上传任何内容。

定义两个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; } 

也许不是最漂亮的…应该是function虽然..

如果/admin/uploadpath是虚拟的,可以使其工作如下:

 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; } 

不是最漂亮的,但工作。