Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add headers to only specific files with nginx

Tags:

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"; } 
like image 257
gakmaku Avatar asked Jun 05 '13 16:06

gakmaku


People also ask

Does Nginx pass headers?

NGINX takes care of known frequently used headers (list of known headers_in). It parses it and stores in the handy place (direct pointer in headers_in ). If a known header may consist of more then one value (Cookies or Cache-Control for example.) NGINX could handle it with an array.

What is Add_header in Nginx?

The Nginx add_header directive allows you to define an arbitrary response header and value to be included in all response codes, which are equal to 200 , 201 , 204 , 206 , 301 , 302 , 303 , 304 , or 307 . This can be defined from within your nginx.

Where do I put Nginx files?

By default the file is named nginx. conf and for NGINX Plus is placed in the /etc/nginx directory. (For NGINX Open Source , the location depends on the package system used to install NGINX and the operating system. It is typically one of /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.)


1 Answers

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)$ {         ...      } } 
like image 74
jmingov Avatar answered Oct 13 '22 20:10

jmingov