# "alias" directive may not be used inside location given

**URL:** <https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200>\
**Category:** NGINX\
**Created:** [July 7, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200 "2010-07-07T14:32:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/1 "2010-07-07T14:32:57Z")

</div>

I don’t understand the following error:

the “alias” directive may not be used inside location given by regular  
expression

From the docs it seems possible:  
[http://wiki.nginx.org/NginxHttpCoreModule#alias](http://wiki.nginx.org/NginxHttpCoreModule#alias)

I’m running 0.7.38

Here is my conflictual declaration:

location ~\* ^/uploads/([\w-/]+.(jpg|m4v|mp4|ogv|png)) {  
alias $rails\_path/uploads/production/$1;  
}

I want to map the request /uploads/assets/myfile.png to  
/uploads/production/assets/myfile.png on disk

Thanks

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/2 "2010-07-07T14:40:31Z")

</div>

On Wed, Jul 07, 2010 at 02:32:57PM +0200, Fernando P. wrote:

> I don’t understand the following error:
> 
> the “alias” directive may not be used inside location given by regular  
> expression
> 
> > From the docs it seems possible:  
> > [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpCoreModule#alias)
> 
> I’m running 0.7.38

This is possible since 0.7.40.

> Here is my conflictual declaration:
> 
> location ~\* ^/uploads/([\w-/]+.(jpg|m4v|mp4|ogv|png)) {  
> alias $rails\_path/uploads/production/$1;  
> }
> 
> I want to map the request /uploads/assets/myfile.png to  
> /uploads/production/assets/myfile.png on disk

In this case you do not need regex:

location /uploads/ {  
alias $rails\_path/uploads/production/;  
}

BTW, it’s better to not use “set $rails\_path /rails;”,  
but set it in alias:

location /uploads/ {  
alias /rails/uploads/production/;  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/3 "2010-07-07T14:41:41Z")

</div>

07.07.10, 16:32, “Fernando P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Here is my conflictual declaration:
> 
> location ~\* ^/uploads/([\w-/]+.(jpg|m4v|mp4|ogv|png)) {

- alias $rails\_path/uploads/production/$1;

- root $rails\_path/uploads/production;
- try\_files /$1 =404;

> }
> 
> I want to map the request /uploads/assets/myfile.png to  
> /uploads/production/assets/myfile.png on disk
> 
> Thanks

–  
br, Denis F. Latypoff.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/4 "2010-07-07T15:51:22Z")

</div>

> location /uploads/ {  
> alias /rails/uploads/production/;  
> }

It doesn’t catch the following request:

/uploads/assets/4/original/paypal\_logo.png

Is there something I didn’t understand?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 4:30pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/5 "2010-07-07T16:30:04Z")

</div>

location ^~ /uploads/ {  
alias /rails/uploads/production/;  
}

Ok I fixed it by adding ^~ , it was a bit further down in the docs. It’s  
much cleaner than using regexps.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/6 "2010-07-07T14:59:50Z")

</div>

Thanks Igor,

Indeed updating Nginx to its latest version solved the problem.

I’ll also edit my conf files according to your recommendations.

Thanks Denis for making me discover about the try\_files declaration,  
it’s interesting to know about it.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 7, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/alias-directive-may-not-be-used-inside-location-given/191200/7 "2010-07-07T17:41:31Z")

</div>

Hello!

On Wed, Jul 07, 2010 at 02:32:57PM +0200, Fernando P. wrote:

> I don’t understand the following error:
> 
> the “alias” directive may not be used inside location given by regular  
> expression
> 
> > From the docs it seems possible:  
> > [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpCoreModule#alias)
> 
> I’m running 0.7.38

Changes with nginx 0.7.40  
09 Mar 2009

```
*) Feature: the "location" directive supports captures in regular
   expressions.

*) Feature: an "alias" directive with capture references may be used
   inside a location given by a regular expression with captures.

```

…

It’s just not in 0.7.38 (which is rather old, and from 0.7.\*  
branch when it wasn’t yet marked stable).

It’s time to upgrade.

Maxim D.
