title: Url matches wrong nginx location
body: <p>Using the nginx conf below, I can't seem to match /api/v2/macs/, but rather it goes to /api/. The issue is that I've tried the same conf in multiple environments and it's failing in my main production environment only. Any ideas?</p>
  <pre><code>server {
  
      listen                      80  default_server;
  
      client_max_body_size        0;
      charset                     UTF-8;
  
      proxy_read_timeout          300;
      proxy_intercept_errors      on;
  
      error_page                  403                         /403;
      error_page                  404                         /404;
      error_page                  502                         /502;
  
      server_tokens               off;
      add_header                  X-Frame-Options             SAMEORIGIN;
  
      # Enable Gzip
      gzip                        on;
      gzip_http_version           1.0;
      gzip_comp_level             2;
      gzip_min_length             1100;
      gzip_buffers                4 8k;
      gzip_proxied                any;
      gzip_types
                                  # text/html is always compressed by HttpGzipModule
                                  text/css
                                  text/javascript
                                  text/xml
                                  text/plain
                                  text/x-component
                                  application/javascript
                                  application/json
                                  image/svg+xml;
  
  
      # Serve index.html
      location / {
          root                    /usr/share/nginx/html;
          index                   index.html index.htm;
          autoindex               off;
          error_page              404                         /404;
          # Cache busting
          if_modified_since       off;
          expires                 -1;
      }
  
      location /api/v2/macs/ {
          rewrite                 ^/api/v2/macs/(.*)            /$1             break;
          proxy_pass              http://my-server:80;
      }
   
      #
      # API
      #
  
      location /api/ {
          rewrite                 ^/api/(.*)                  /api/$1         break;
          proxy_pass              http://other-server;
      }
  
      #
      # Ping
      #
  
      location = /ping {
          return 200;
      }
  
  
      #
      # Errors
      #
  
      location = /403 {
          root                    /usr/share/nginx/html/templates/error;
          index                   403.html;
      }
      location = /404 {
          root                    /usr/share/nginx/html/templates/error;
          index                   404.html;
      }
      location = /502 {
          root                    /usr/share/nginx/html/templates/error;
          index                   maintenance.html;
      }
  
  }
  </code></pre>
  <p>According to my understanding, nginx should match using the longest prefix before stopping, but that's not what's happening.
  Thanks!</p>