如何使用nginx仅向特定文件添加标头

gak*_*aku 16 header nginx

我有图片,我想将它们的标题添加到最大,我有可以更改和发布图片的个人资料图片,我想只为帖子图片添加标题,但不是为了个人资料图片,我不知道我怎么能管理这个.谢谢,这是我的配置,

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

this is the path of users, /user/name-of-the-picture.jpg
Run Code Online (Sandbox Code Playgroud)

我只想在邮件路径中添加标题

location ~* \.(css|js|png|gif)$ {
   expires max;
   add_header Pragma public;
   add_header Cache-Control "public";
}
Run Code Online (Sandbox Code Playgroud)

jmi*_*gov 28

目前我们有两种方法可以解决这个问题:

选项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;
    .
    .
    .
}
Run Code Online (Sandbox Code Playgroud)

选项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)$ {
        ...

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,不知道你可以嵌套`location`集团! (4认同)