17

I have an /admin catalog on my website and I would like to allow admins to upload large files via web forms. Here is how my nginx.com looks:

http {
    # ...
    client_max_body_size 16M;
    # ...

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

        location /admin {
            client_max_body_size 256M;
        }

        # ...
    }
}

This does not work. /admin/index.php script cannot upload files larger that 16Mb: 413 Request Entity Too Large

However, if I move client_max_body_size to server block everything works fine. But I wouldn't want to make this change only for admin catalog.

According to docs, client_max_body_size can be placed inside location block to override setting only for desired path.

What could be wrong?

Temnovit
  • 1,107
  • 6
  • 19
  • 27

1 Answers1

18

It works just fine, the problem is you have misunderstood how locations work. Nginx will only ever apply one location block, never more than one. So when you have two locations

location ~ \.php$ and location /admin and the URI is /admin/index.php then your first location applies but the second one doesn't. Even if you were to use a rewrite within a location then nginx would discard directives and reparse them for the new location.

This is also the reason why you always post full configs so that you don't hide what's actually wrong.

Martin Fjordvald
  • 7,589
  • 1
  • 28
  • 35
  • Sorry, for not posting complete config, you are right. As for the answer, this can't be true. Here is the example from docs: http://wiki.nginx.org/HttpCoreModule#location And in other places multiple location blocks work for me just fine. – Temnovit Aug 30 '11 at 11:38
  • 1
    Sorry, but it's 100% right. Nginx can search through multiple location blocks, but it will only ever apply the directives of one of them. Which one it chooses depends on the rules documented on the page you linked. – Martin Fjordvald Aug 30 '11 at 15:22
  • @MartinFjordvald what is the solution for this ? – chaosguru May 09 '19 at 09:20
  • Look at `configuration E` in the link: http://nginx.org/en/docs/http/ngx_http_core_module.html#location – holmberd May 29 '19 at 18:24
  • "Even if you were to use a rewrite within a location then nginx would discard directives and reparse them for the new location." This is very important, too! Are details about this in the official docs? – Juanitocalero Feb 06 '20 at 11:23
  • 1
    @Juanitocalero not really no, the official documentation is not a very good starter guide, more of a reference documentation for syntax and basic info. – Martin Fjordvald Feb 11 '20 at 16:10