0

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:

3
  • 1
    You have told us your proposed method for splitting things, but you haven't said what you are trying to achieve. What you've suggested doesn't seem possible, and without a reason / goal it seems fairly pointless. Suggest you edit your question to include what you're trying to achieve
    – Tim
    May 11, 2021 at 7:23
  • 1
    Automatic merging of location blocks is likely not implemented because that would create very confusing set of rules how the mergins is actually performed. And then again, the merging rules would be way too complex for anybody to understand properly. You should use a configuration management system to generate the configuration, there you can implement re-use patterns like this. May 11, 2021 at 15:49
  • @Tim Oh, in fact, I'm not trying to achieve anything practical. I was asking about this for curiosity and wanted to see if Nginx really can't do it in the way I mentioned or it can but I wasn't aware of it.
    – yaobin
    May 12, 2021 at 1:46

1 Answer 1

2

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

3
  • Thanks for the answer! My bad. I should have mentioned that I didn't mean to use include. I was trying to see if the location block for, say, /, can be divided into different configuration files (without using include) and they still get merged into one block when Nginx parses the configuration files.
    – yaobin
    May 11, 2021 at 2:57
  • 1
    Just read the first word of my answer then :)
    – Tim
    May 11, 2021 at 7:23
  • @yaobin It doesn't work that way. It does work with include. Jun 9, 2021 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .