Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Nginx Alias to One Location

Tags:

alias

regex

nginx

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.

like image 560
Tux Avatar asked Feb 27 '12 01:02

Tux


1 Answers

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.

like image 53
Qtax Avatar answered Oct 08 '22 20:10

Qtax