nginx 别名语法

发布于 2024-12-12 04:41:19 字数 332 浏览 0 评论 0原文

我正在尝试使用以下语法为网站中某些图片的位置添加别名:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2024-12-19 04:41:19

服务器位置:

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

在favs目录上:

$ 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

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
早茶月光 2024-12-19 04:41:19

您的正则表达式不正确。首先,位置类型指示符 ~ 和正则表达式的开头之间需要有一个空格。其次,^ 表示字符串的开头,因此 /^ 永远不会匹配任何内容。此外,由于您要从文件名中删除扩展名,我相信 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;
}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文