title: The last & break mechanism in nginx rewrite
body: <p>Am now dealing with nginx rewrite for my project but got something out of my expectation, just share with you to see if any reasonable advice on this.</p>
  
  <p>The nginx server setting list below:</p>
  
  <pre><code>location /download/ {
      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3;
      rewrite ^(/download/.*)/movie/(.*)\..*$ $1/avi/$2.mp3 break;
      rewrite ^(/download/.*)/avvvv/(.*)\..*$ $1/rmvb/$2.mp3;
  }
  </code></pre>
  
  <p>From above section, you can see that I only add one download location with three rewrite rules.I open the debug log in nginx and start it to load this setting.</p>
  
  <p>Now we enter url: localhost/download/123/movie/UBW.avi in our browser. From the log, we can see that the rules are hit, logs below:</p>
  
  <pre><code>2017/05/22 15:27:27 [notice] 1904#12520: *85 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:27:27 [notice] 1904#12520: *85 "^(/download/.*)/movie/(.*)\..*$" matches "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:27:27 [notice] 1904#12520: *85 rewritten data: "/download/123/avi/UBW.mp3", args: "", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:27:27 [error] 1904#12520: *85 CreateFile() "D:\Soft\nginx-1.13.0/html/download/123/avi/UBW.mp3" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  </code></pre>
  
  <p>From above info, we can see that the second rule is hit and the section break due to the "break" keyword. This behavior is in our expectation.</p>
  
  <p>But wen I changed the section like below and behave the same behavior as above:</p>
  
  <pre><code>location /download/ {
      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3;
      rewrite ^(/download/.*)/movie/(.*)\..*$ $1/avi/$2.mp3 last;
      rewrite ^(/download/.*)/avvvv/(.*)\..*$ $1/rmvb/$2.mp3;
  }
  </code></pre>
  
  <p>The log below:</p>
  
  <pre><code>2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/movie/(.*)\..*$" matches "/download/123/movie/UBW.avi", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [notice] 11244#11544: *92 rewritten data: "/download/123/avi/UBW.mp3", args: "", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/media/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/movie/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [notice] 11244#11544: *92 "^(/download/.*)/avvvv/(.*)\..*$" does not match "/download/123/avi/UBW.mp3", client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  2017/05/22 15:33:45 [error] 11244#11544: *92 CreateFile() "D:\Soft\nginx-1.13.0/html/download/123/avi/UBW.mp3" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /download/123/movie/UBW.avi HTTP/1.1", host: "localhost"
  </code></pre>
  
  <p>From the second row of the log, we can see that the rule is hit, but then nginx start a new loop for the download section, the strange thing is that , it didn't hit the rule, why? </p>
  
  <p>Also, if we use last keyword, then how many times will the nginx try to loop the section, in default it's two, right? </p>