nginx别名语法

我试图使用下面的语法别名在网站中的一些照片的位置:

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

是正确的,或者我在nginx的access.log中错过了其他的东西我得到这个消息:

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

但在浏览器中显示索引页面

此外,我很困惑别名和重写在nginx,我应该使用

服务器位置:

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

在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 

你的正则表达式是不正确的。 首先,在位置类型指示符〜和正则表达式的开始之间需要一个空格。 其次,^表示字符串的开始,所以/ ^将永远不匹配任何东西。 另外,由于你从文件名剥离了扩展名,我相信nginx会把这个文件作为默认的MIME类型来提供,所以你应该在这个位置设置default_type image / png:

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

编辑:我误解了什么是必需的。 要更改以/ apple-touch-icon开始的任何内容的根目录,请使用:

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

这是一个前缀匹配,不会被正则表达式位置覆盖。