如何使用Nginx将标头仅添加到特定文件 [英] How to add headers to only specific files with nginx

查看:58
本文介绍了如何使用Nginx将标头仅添加到特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图片,我想将其标题添加到最大,我有可以更改的个人资料图片并张贴图片,我只想为张贴图片添加标题,而不是个人资料图片,我不知道如何我可以管理这个吗?谢谢,这是我的配置,

I have pictures, and I want to add their headers to max, I have profile pictures which can be changed and post pictures, I want to add headers only for post pictures, but not to profile pictures, I have no idea how can I manage this. thank you, this is my configuration,

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

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

我只想将标题添加到发布路径

I only want to add headers to post path

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

推荐答案

当前,我们有两个选项可以解决此问题:

Currently we have two options to solve this:

选项1:

重复的位置:NGINX寻找最佳匹配. (性能好一点)

Duplicated locations: NGINX looks for the best match. (a little better performance)

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:

嵌套位置:在内部位置块中按扩展名过滤

Nested locations: Filtered by extension in the inner location blocks

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

    }
}

这篇关于如何使用Nginx将标头仅添加到特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆