【问题标题】:How to add headers to only specific files with nginx如何使用 nginx 仅将标头添加到特定文件
【发布时间】:2013-06-05 16:31:24
【问题描述】:

我有图片,我想将它们的标题添加到max,我有可以更改和发布图片的个人资料图片,我只想为发布图片添加标题,而不是为个人资料图片添加标题,我不知道如何我可以管理这个吗?谢谢,这是我的配置,

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";
}

【问题讨论】:

    标签: nginx header


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,不知道可以嵌套location blocs!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多