Re: "I thought location blocks couldn't be nested inside other location blocks?"
I had to refresh my mind, but nesting locations seems valid to do - though it also seems tricky sometimes: https://github.com/pagespeed/ngx_pagespeed/issues/387

There's also the way how `if` is implemented under the hood when used within a location (as nested locations).