title: How to make map-derived variable reflect the current $uri?
body: <p>A request for <code>/olddir1/img.jpeg</code> using the following nginx configuration will do a correct internal redirect to <code>/newdir1/img.jpeg</code> but it will leave the variable <code>$extra_uri</code> set to the value it would have during the first rewrite cycle, that is <code>/olddir1/img.jpeg</code>.</p>
  
  <p>Just before executing the <code>rewrite</code> directive, <code>$alias_uri</code> and <code>$extra_uri</code> will have the following values, all correct:</p>
  
  <ul>
  <li><code>$alias_uri</code>: <code>/newdir1/img.jpeg</code></li>
  <li><code>$extra_uri</code>: <code>/olddir1/img.jpeg</code></li>
  </ul>
  
  <p>After the <code>rewrite</code> directive is run, setting $uri to <code>/newdir1/img.jpeg</code>, the variables will have the following values:</p>
  
  <ul>
  <li><code>$alias_uri</code>: <code>/newdir1/img.jpeg</code></li>
  <li><code>$extra_uri</code>: <code>/olddir1/img.jpeg</code></li>
  </ul>
  
  <p><code>$alias_uri</code> has the correct value (nothing has matched in the <code>map</code>, so the default is being used, i.e. the current <code>$uri</code>). <code>$extra_uri</code> instead has a stale value.</p>
  
  <p><strong>How come <code>$extra_uri</code> isn't set to the new <code>$uri</code>?</strong></p>
  
  <pre><code>server {
      listen 80 default_server;
  
      server_name     'test.example.local';
  
      location / {
          root '/var/www/test/content';
  
          set $alias_uri $example__alias_uri;
          set $extra_uri $example__extra_uri;
  
          if ($alias_uri != $uri) {
              rewrite ^ $alias_uri last;
          }
  
          add_header X-Alias "uri: &gt;$uri&lt; alias_uri: &gt;$alias_uri&lt;" always;
          add_header X-Extra "uri: &gt;$uri&lt; extra_uri: &gt;$extra_uri&lt;" always;
  
          try_files $uri =404;
  }
  
  map $uri $example__alias_uri {
      default $uri;
  
      ~/olddir1(?&lt;file&gt;/[^/]*)$ /newdir1$file;
      ~/olddir2(?&lt;file&gt;/[^/]*)$ /newdir2$file;
  }
  
  map $uri $example__extra_uri {
      default $uri;
  }
  </code></pre>