nginx - add module for specific location using inheritance

Viewed 715

I want to increase the client_max_body_size parameter for a specific location.

I tried to do something like that in the location

location / {
    proxy_pass http://dashboard-app;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto https;
    proxy_cache_bypass $http_upgrade;

    location /admin/upload {
        client_max_body_size 10m;
    }
}

the problem is that there is no proxy_pass or all other modules in the /admin/upload location, so I'm gettings 404 for that route.

I know I can just add all the modules inside that location, but it's just a duplication of code which seemds useless to me.

How can I inherit the base location modules to the upload location conf?

1 Answers

Which modules are inherited and which are not?

This issue is not expressed clear in the documentation. You need to check each and every directive individually.

In general, commands are not inherited, options are inherited, arrays of options ("array-type directives") are inherited but replaced at whole (for example, no way to add header in a descender, all headers will be replaced).

See: Directive Inheritance in Nested Location Blocks answer.

How to avoid code duplication?

From nginx side only include directive can be used for that. Also, a recommended way is to use third party tools (e.g. template engines) to generate nginx config from a concise source.

Related