Search code examples
regexnginxrules

How to write nginx rewrite rule to catch /static in relative path


I want to rewrite following URLs. There can be several paths before static. So I want to remove all of them and just get the url starting with /static

*/static/js/52.eebbab07.chunk.j
someurl/subpath1/subpath2/static/js/52.eebbab07.chunk.j

to this

/static/js/52.eebbab07.chunk.j

How can I do that?


Solution

  • How about:

    rewrite /static/(.*)$ /static/$1 last;
    

    The rewrite directive allows to capture trailing part of URI after /static, and the substitution (second argument) can use it as $1.