Nginx how to add a header if it is not installed

I want to add the nginx (Cache-control) header, unless it is installed.

I need to increase the cache time (use php) in some case and β€œsay” nginx through the header.

Sorry if this is not clear, I'm really a beginner)

+6
source share
1 answer

You can use map to populate the $cachecontrol variable. If $http_cache_control (client header) is empty, set a custom value. Otherwise (default) reuse the value from the client.

 map $http_cache_control $cachecontrol { default $http_cache_control; "" "public, max-age=31536000"; } 

You can then use this variable to send the upstream header.

 proxy_set_header X-Request-ID $cachecontrol; 

For the next question from jmcollin92, I wrote the following in the SO Documentation, which is now transcribed here.

X-Request-ID

Nginx

Reverse proxies can determine if the client contains the X-Request-ID header and sends it to the server. If such a header is not provided, it may provide a random value.

 map $http_x_request_id $reqid { default $http_x_request_id; "" $request_id; } 

In the above code, the request identifier is stored in the $reqid variable, from where it can subsequently be used in logs.

 log_format trace '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" "$http_user_agent" ' '"$http_x_forwarded_for" $reqid'; 

It must also be transferred to backend services.

 location @proxy_to_app { proxy_set_header X-Request-ID $reqid; proxy_pass http://backend; access_log /var/log/nginx/access_trace.log trace; } 
0
source

All Articles