2

This is related to this question, but the answer did not work for me.

I need to turn this: /api/batch.json?param=1

into /batch?param=1&format=json

Nginx location:

location /api/batch {
   proxy_set_header   X-Real-IP        $remote_addr;
   proxy_set_header   Host             $http_host;
   proxy_pass         http://localhost:8000/batch;
}

How can I do this?

Werner Raath
  • 1,322
  • 3
  • 16
  • 34

1 Answers1

1

Use rewrite...break to change the URI within a location before passing it upstream using proxy_pass.

For example:

location /api/batch {
    ...
    rewrite ^/api(/batch)\.(json)$ $1?format=$2 break;
    proxy_pass  ...;
}

The rewrite directive will automatically append the original parameters (if any) unless the replacement string ends with a ?. See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81