Multiple Nginx Alias to One Location

13,824

Try:

location ~ ^/(?:styles|css)/(.*)$ { alias /var/www2/styles/$1; }

Or

location ~ ^/(styles|css)/(.*)$ { alias /var/www2/styles/$2; }

$1 refers to the first capturing group (...). When you added another group it referred to that one instead. You can use a non-capturing group (?:...) instead, or refer to the second capturing group $2.

Share:
13,824
Tux
Author by

Tux

Updated on June 04, 2022

Comments

  • Tux
    Tux almost 2 years

    I would like to create a location rule for two alias to one location.

    This is the rule used for one location:

    location ~ ^/images/(.*)$ { alias /var/www2/images/$1; }
    

    What I would like to do is define two alias in location. So for example, I can visit http://domain.com/styles/file.css and http://domain.com/css/file.css and it would go to one alias which is /var/www2/styles/

    I've tried something like this, but it did not work for me.

    location ~ ^/(styles|css)(.*)$ { alias /var/www2/styles/$1; }
    

    But the again, I don't know regex much.

  • Tux
    Tux about 12 years
    Now I know this is a bit overkill, but how would I also allow only css file extension in this location and nothing else even if there is other files in my /styles/ folder ? Never mind got it. location ~ ^/(?:styles|css)/(.*\.css)$
  • Tux
    Tux about 12 years
    This of course works only with one extension, how would I add more then one ?
  • Tux
    Tux about 12 years
    I got it with location ~ ^/(?:icons|ico)/(.*\.(png|gif|ico))$ { If you know a better way please reply.
  • Qtax
    Qtax about 12 years
    @Tux, that looks fine. Altho you can use a non-capturing group instead of the capturing one: ^/(?:icons|ico)/(.*\.(?:png|gif|ico))$