How to add headers to only specific files with nginx

后端 未结 1 1728
心在旅途
心在旅途 2021-02-05 09:36

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

相关标签:
1条回答
  • 2021-02-05 10:18

    Currently we have two options to solve this:

    Option 1:

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

    Option 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)$ {
            ...
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题