如何在nginx中仅为特定文件添加标头

24

我有一些图片,想要将它们的标题添加到最大化(即使窗口被放大也能看到完整标题),其中有可以更改的个人资料图片以及帖子图片,我只想为帖子图片添加标题而非个人资料图片,但是我不知道该如何管理。谢谢,以下是我的配置:

this is the path of posts, /post/name-of-the-picture.jpg

this is the path of users, /user/name-of-the-picture.jpg

我只想为帖子路径添加标题

location ~* \.(css|js|png|gif)$ {
   expires max;
   add_header Pragma public;
   add_header Cache-Control "public";
}
1个回答

37

目前我们有两个选项来解决这个问题:

选项1:

重复的位置:NGINX 寻找最佳匹配(性能稍微更好)。

location /post/ {
    post config stuff;
    .
    .
    .
}    
location ~* ^/post/.*\.(css|js|png|gif)$ {
    post/files.(css|js|png|gif) config stuff;
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public";
}
location /user/ {
    user folder config stuff;
    .
    .
    .
}    
location ~* ^/user/.*\.(css|js|png|gif)$ {
    user/files.(css|js|png|gif) config stuff;
    .
    .
    .
}

选项2:

嵌套的位置:在内部位置块中按扩展名进行筛选

location /post/{
    ...

    location ~* \.(css|js|png|gif)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public";
    }
}
location /user/{
    ...

    location ~* \.(css|js|png|gif)$ {
        ...

    }
}

4
谢谢,我不知道你可以嵌套 location 块! - Mr.Coffee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接