1

I am quite new to nginx rewrite concept and I can't figure out how to rewrite the requested_uri to a specific Uri. I am trying to make rewrites to specific js files.

For example, I have this requested_uri: http://test.example.com/mf/test/index.js?review=abc I want to rewrite it only to /index.js

I am using my own specific domain for that. I want to rewrite everything to /index.js so that I can get a js file as a response.

I am really struggling to find out how it works. Can anyone help me do this, please?

dimpap
  • 11
  • 2
  • 1
    Are you trying to drop the query string or keep it in the request? – Paul Aug 18 '22 at 14:25
  • 1
    Please add output of `nginx -T` to the question so that your configuration is visible. – Tero Kilkanen Aug 19 '22 at 16:48
  • https://stackoverflow.com/questions/9641603/remove-parameters-within-nginx-rewrite – gapsf Aug 20 '22 at 09:29
  • @Paul I am trying to drop the query string. – dimpap Aug 20 '22 at 17:20
  • @gapsf I am also trying to remove the part before index.js except from the query string. – dimpap Aug 20 '22 at 17:28
  • I think something like `location ^~ /mv/test/index.js { rewrite ^/index.js$ $scheme://test.example.com/index.js?; }` or optionally add `permanent;` parameter on the end if you want to have nginx respond with 301 Moved Permanently. – Paul Aug 20 '22 at 17:59

2 Answers2

0

I think this will work:

location ^~ /mv/test/index.js {
    rewrite ^/index.js$ $scheme://test.example.com/index.js?;
}

Add permanent if you want it to be a 301 Moved Permanently:

location ^~ /mv/test/index.js {
    rewrite ^/index.js$ $scheme://test.example.com/index.js? permanent;
}
Paul
  • 2,755
  • 6
  • 24
  • 35
  • I want to remove also the part $scheme://test.example.com/. After the rewrite the link should be /index.js only without anything else. I proxy it to a microservice in order to get back a JS file. – dimpap Aug 22 '22 at 10:09
  • Please post at least the entire server block that `rewrite` is in and all other relevant configurations. – Paul Aug 22 '22 at 13:05
0

Rewrite to last part

rewrite ^.*\/(.+)\?.*$/ $1
gapsf
  • 641
  • 1
  • 5
  • 12