Eslam
Eslam

Reputation: 1653

nginx alias syntax

i'm trying to alias the location of some pics in the website using the following syntax :

location ~/^apple-touch-icon(.*)\.png$ {
 alias /opt/web_alpha/img/$1;
 } 

is that right or I missied something else in access.log of nginx I got this message :

"GET /apple-touch-icon.png HTTP/1.1" 404 142

but in the browser it shows me the index page

moreover i'm confused regarding alias and rewriting in nginx , which should I use

Upvotes: 2

Views: 7041

Answers (2)

GoTLiuM
GoTLiuM

Reputation: 791

server location:

location ~ ^/(apple-touch-icon|browserconfig|favicon|mstile)(.*)\.(png|xml|ico)$ {
    root /home/user/project/static/images/favs;
}

on favs dir:

$ ls -1 static/images/favs/

apple-touch-icon-114x114.png
apple-touch-icon-120x120.png
apple-touch-icon-144x144.png
apple-touch-icon-152x152.png
apple-touch-icon-57x57.png
apple-touch-icon-60x60.png
apple-touch-icon-72x72.png
apple-touch-icon-76x76.png
apple-touch-icon-precomposed.png
apple-touch-icon.png
browserconfig.xml
favicon-160x160.png
favicon-16x16.png
favicon-196x196.png
favicon-32x32.png
favicon-96x96.png
favicon.ico
mstile-144x144.png
mstile-150x150.png
mstile-310x150.png
mstile-310x310.png
mstile-70x70.png

Upvotes: 5

kolbyjack
kolbyjack

Reputation: 18290

Your regex is incorrect. First, you need a space between the location type indicator ~ and the start of the regex. Second, ^ denotes the beginning of the string, so /^ will never match anything. Additionally, since you're stripping the extension from the filename, I believe nginx will end up serving the file as the default mime type, so you should probably set default_type image/png in the location:

location ~ ^/apple-touch-icon(.*)\.png$ {
  default_type image/png;
  alias /opt/web_alpha/img/$1;
}

EDIT: I misunderstood what was required. To just change the root for anything that starts with /apple-touch-icon, use:

location ^~ /apple-touch-icon {
  root /opt/web_alpha/img;
}

That's a prefix match that won't be overridden by a regex location.

Upvotes: 2

Related Questions