title: How to re-use NGINX proxy settings in several locations
body: <p>I have the need to restrict access to certain files based on a query string parameter. I have an NGINX proxy server that sits in front of several other nginx web servers for load balancing. I have decided to enforce this query string parameter at the proxy server level in order to consolidate the configuration changes. This has added a bit of complexity to my setup because the request can not get trapped in the if as it needs to be sent upstream.</p>
  
  <pre><code>server {
          listen 443;
          # SSL Settings
  
          server_name staging.xxxx.com;
  
          location / {
                  proxy_pass http://webdav-cluster;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header X-Real-IP       $remote_addr;
                  proxy_set_header Host            $http_host;
          }
  
          # Unless the correct application token is passed in as a query parameter
          # then deny access.
          location ~ \/protected\/.*txt$ {
                  if ($arg_secret != abc) {
                          return 403;
                  }
  
                  proxy_pass http://webdav-cluster;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header X-Real-IP       $remote_addr;
                  proxy_set_header Host            $http_host;
          }
  }
  </code></pre>
  
  <p>Is there a way to store those 4 proxy lines in a location or variable and then internally redirect to that with one line? I may also use the same settings in different virtual hosts.</p>