0

I am trying to write a regular expression such as it has /admin in the middle and ends with .css or .js or other formats.

http://example.com/admin/static/style.css (SHOULD WORK)

http://example.com/admin/static/vendor.js (SHOULD WORK)

http://example.com/static/style.css (SHOULD NOT WORK)

I am trying to use this in nginx location block. I tried this

location /admin/\.(css|js)${

}

but not working.

Any ideas?

  • 1
    Where do you want to use it for what? What have you tried? How did this fail? – Sven May 9 at 12:39
  • I updated the question. – Nika Khurashvili May 9 at 12:43
  • Migrate this to StackOverflow? But in the mean time see if this helps. – ShapeOfMatter May 9 at 13:09
  • @ShapeOfMatter: Why migrating this? Writing regexes for web servers is a sysadmin topic... – Sven May 9 at 13:13
  • The problem in your example is missing ~ after location. Without tilde, it is a simple prefix match, not a regex match. – Tero Kilkanen May 9 at 21:59
0

What you may be missing is the section between "admin" and "css/js". This could work:

(.*)\/admin(.*)\.(css|js)

I also added the (.*) in the beginning, meaning that in front of /admin there may be additional characters.

0

Please try this for a case insensitive match (~*):

location ~* ^/admin/.+\.(css|js)$ {
    # more configuration
}

Add other file types separated by | like ^/admin/.+\.(css|js|png|gif|jpe?g)$.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.