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?

4
  • Indeed I lost param-1 Commented Nov 21, 2019 at 11:32
  • That didn't want to work, so I've updated my post. Commented Nov 21, 2019 at 12:00
  • 1
    Try: rewrite ^/api(/batch)\.(json)$ $1?format=$2 break; inside the location block. Commented Nov 21, 2019 at 12:03
  • Brilliant, works like a charm. You are welcome to post it as an answer Commented Nov 21, 2019 at 12:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.