Nested Location Nginx Configuration

You may only define nested prefix locations when the parent location is also a prefix location.

A guide from stackoverflow shows this basic configuration.

location /a {  
        location /a {
               # You can also skip this location and just write
               # your code directly under the parent location
        }
        location /a/b {
        ...
        }
}

When a parent location is defined by a regex, any nested locations must also be defined by regexes:

location ~ ^/(a|b) {  
        location ~ ^/a {
        ...
        }
        location ~ ^/b {
        ...
        }
}

However, you may also define nested regex locations when the parent location is a prefix location:

location /a/b {  
        location ~ /a {
        ...
        }
        location ~ /b {
        ...
        }
}

And then I apply this to load balancer configuration in our production infrastructure.

    location ~ ^/(hdr|uploads|ast|sitemap)/ {
        add_header  Cache-Control "public";
        add_header  Cache-Control "max-age=365000000, immutable";
        etag on;
        expires max;
        proxy_pass http://riak.bukalapak.com;
        location ~ ^/uploads/loan/btpn {
          add_header  Cache-Control "public";
          add_header  Cache-Control "max-age=365000000, immutable";
          etag on;
          expires max;
          proxy_pass http://riak.bukalapak.com;
          auth_basic "Restricted";
          auth_basic_user_file /etc/nginx/.btpn-htpasswd;
        }
        location ~ ^/uploads/loan/modalku {
          add_header  Cache-Control "public";
          add_header  Cache-Control "max-age=365000000, immutable";
          etag on;
          expires max;
          proxy_pass http://riak.bukalapak.com;
          auth_basic "Restricted";
          auth_basic_user_file /etc/nginx/.modalku-htpasswd;
        }
    }

Lukman

Radio astronomer pretending to be system engineer.

Bandung, Indonesia