title: How to have multiple location blocks use the same named @location
body: <p>I am trying to create a nginx conf file that has little repetition in it. I am using nginx to serve static files, and it proxies 404s or php content to the named location @varnish:</p>
  
  <pre><code>location @varnish {
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass_header Set-Cookie;
      proxy_pass http://localhost:6081;
      proxy_set_header Request-URI $request_uri;
  }
  </code></pre>
  
  <p>For the "standard" situation whereby nginx should check to see if it has a file and then pass through to the backend, the following works fine:</p>
  
  <pre><code>location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
      access_log        off;
      add_header      Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
      expires           max;
      open_file_cache_valid 120m;
      try_files $uri @varnish;
  }
  </code></pre>
  
  <p>However, for PHP, I don't even want it to try the file, it should just immediately redirect the request to @varnish:</p>
  
  <pre><code>location ~ \.php$ {
      rewrite . @varnish last;
  }
  </code></pre>
  
  <p>However, this does not appear to work. It seems a pain to have two separate near identical blocks (one for @backend and one for php) both referencing the same proxy, and is the sort of issue where humans can forget to put something in one and not the other.</p>