1

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?

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

2 Answers 2

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)$.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.