title: Nginx rewrite url path to add prefix of path
body: <p>I am working with my nginx server and I have no idea how to add /api prefix to the existing url.</p>
  
  <p>I have two endpoints /api/deepzoom and /api/detection exposed by Flask.
  I don't want to change the code in frontend that call /deepzoom and /detection yet. </p>
  
  <p>How can I rewrite/redirect the url path to /api/deepzoom when called from frontend with /deepzoom </p>
  
  <p>My current snippet in nginx.conf:</p>
  
  <pre><code>upstream platform {
      server platform:5001;
  }
  upstream models {
      server models:4999;
  }
  
  upstream deepzoom {
      server deepzoom:5999;
  }
  
  server {
      listen 80 ;
      server_name  myhost.mydomain.com;
  
      client_max_body_size    0;
      client_body_buffer_size 1m;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   Host $host:$server_port;
      proxy_set_header   X-Real-IP  $remote_addr;
      send_timeout       10m;
      proxy_buffer_size 512k;
      proxy_buffers 4 1024k;
      proxy_busy_buffers_size 1024k;
      proxy_redirect off;
  
  location ^~ /api/detection/ {
      include uwsgi_params;
      proxy_pass    http://models;
  }
  
  location ^~ /api/deepzoom/ {
      include uwsgi_params;
      proxy_pass    http://deepzoom;
  }
  location  / {
      include uwsgi_params;
      proxy_pass         http://platform;
  
      }
   }
  </code></pre>
  
  <p>I have tried to add the lines in server block:</p>
  
  <pre><code>rewrite ^ /detection/ http://$server_name/api/detection/$1 permanent;
  rewrite ^ /deepzoom/ http://$server_name/api/deepzoom/$1 permanent;
  </code></pre>
  
  <p>but it doesn't work with 404 Not Found error.</p>
  
  <p>Anyone can help me to figure it out and how to satisfy the requirement. Thanks!</p>