nginx 别名语法
我正在尝试使用以下语法为网站中某些图片的位置添加别名:
location ~/^apple-touch-icon(.*)\.png$ {
alias /opt/web_alpha/img/$1;
}
是这样还是我错过了其他内容 在 nginx 的 access.log 中我收到以下消息:
“获取/apple-touch-icon.png HTTP/1.1”404 142
但在浏览器中它向我显示了索引页面
而且我对 nginx 中的别名和重写感到困惑,我应该使用哪个
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务器位置:
在favs目录上:
$ ls -1 static/images/favs/
server location:
on favs dir:
$ ls -1 static/images/favs/
您的正则表达式不正确。首先,位置类型指示符 ~ 和正则表达式的开头之间需要有一个空格。其次,^ 表示字符串的开头,因此 /^ 永远不会匹配任何内容。此外,由于您要从文件名中删除扩展名,我相信 nginx 最终会将文件作为默认 mime 类型提供服务,因此您可能应该在以下位置设置 default_type image/png:
编辑:我误解了所需的内容。要更改以 /apple-touch-icon 开头的任何内容的根,请使用:
这是一个不会被正则表达式位置覆盖的前缀匹配。
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:
EDIT: I misunderstood what was required. To just change the root for anything that starts with /apple-touch-icon, use:
That's a prefix match that won't be overridden by a regex location.