NGINX: Is it possible to split the `location` configuration block into multiple blocks or even into files?

Update: Interesting.. Those who downvoted my question could you also leave a comment about why you downvoted it? I believe my question is asked way better than many other questions that didn't receive downvotes.


Update: I didn't mean to use the include directive.


Many questions on Stack Exchange are about how to extract common location configuration into a shared place, such as this.

But I'm trying to achieve the opposite: Is it possible to split the configuration for one location into multiple files?

In my project, the Nginx server is set up with the default configuration for the location / in /etc/nginx/conf.d/default.conf:

# Block A

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    ... ...

Now I want to add the header Cache-Control no-cache to the location /. I know I can do it by simply adding add_header Cache-Control no-cache always; to make the block look like:

# Block B

    ... (same as above)

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header Cache-Control no-cache always;
    }

    ... ...

I'm wondering if it is possible to split the location / configuration into two files. For example, keeping the current /etc/nginx/conf.d/default.conf unchanged (as shown in Block A), Can I add another file, say, /etc/nginx/conf.d/cache-control.conf with the content:

# Block C

server {
    listen       80;
    listen  [::]:80;

    location / {
        add_header Cache-Control no-cache always;
    }

And then at run time Nginx can merge the two configuration blocks into one location block as shown in Block B?

I didn't succeed in achieving this in my experiments. By reading [1] and [2], I think this cannot be achieved because Nginx treats all the location blocks as separate configuration blocks and once a block matches the request URL, this block is used to serve the request and the other blocks are skipped. Although [2] discusses four cases that may cause location redirection, none of the four cases happens in the configuration here.

But I'm still wondering if this really can't be achieved in Nginx or it may be that I haven't learned Nginx well enough.

References:

  • [1] Nginx documentation on location
  • [2] Understanding Nginx Server and Location Block Selection Algorithms

Solution 1:

No, but you can use the include keyword to include shared blocks of code. I'm not sure that's what you're trying to do or not, your question is a bit unclear to me.

Here's some code I use

server {
  server_name example.com;
  listen 443 ssl http2;
  root /var/www;

  ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
  include snippets/tls-config.conf; 
  (etc)

Then the TLS config is in that external file