Home > Enterprise >  Replace one of the segment with a wildcard in a simple way?
Replace one of the segment with a wildcard in a simple way?

Time:05-25

I have this code in nginx config:

 location ~ ^/assets/(static|dynamic)/(images|styles|scripts|fonts|videos|audios|var)/(.*) {
     alias /var/web/app1/assets/$1/$2/$3;
     autoindex off;
     expires 366d;
 }

Now I want to replace the (images|styles|scripts|fonts|videos|audios|var) with a wildcard. And everything should continue to work as is. That is, in that segment there could be a directory with any name whatsoever.

Is there a simple way to do it in a simple way? Preferably without a regexp.

CodePudding user response:

If you want to replace this part (images|styles|scripts|fonts|videos|audios|var) with wildcard, you can just remove that part and only keep (.*) in location.

Like this:

location ~ ^/assets/(static|dynamic)/(.*)$ {
    alias /var/web/app1/assets/$1/$2;
    autoindex off;
    expires 366d;
}

Update: Here is another approach, while keeping 4 segments, but using regex:

location ~ ^/assets/(static|dynamic)/([^/] )/([^/] )$ {
    alias /var/web/app1/assets/$1/$2/$3;
    autoindex off;
    expires 366d;
}
  • Related