Trying to add an Expires header in Nginx on a Plesk server like this is problematic.
1 2 3 |
location ~* .(txt|xml|js)$ { expires 1y; } |
The “location” directive conflicts (overrides) the “location” directive created by Plesk. The work around is to disable “Serve static files directly by nginx” but this is a valuable feature that we usually don’t want to turn off.
An alternative is to use a “map” directive. Setup the map in the global nginx config by creating /etc/nginx/conf.d/expires_map.conf with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Custom expires map map $sent_http_content_type $expires { default off; text/html 1y; text/css 1y; text/xml 1y; application/xml 1y; text/javacript 1y; application/pdf 1y; application/vnd.ms-powerpoint 1y; application/x-shockwave-flash 1y; application/msword 1y; application/vnd.ms-fontobject 1y; # .flv, .avi, .mov, .wmv, .mp4, .m4v, .ogg, .webm ~video/ 1y; # .mp3, .wav, .aac ~audio/ 1y; # .ico, .jpg, .jpeg, .png, .gif, .webp, .svg ~image/ 1y; # .ttf, .otf, .woff, woff2 ~font/ 1y; } |
Next in the “Additional nginx directives” field add:
1 2 |
# assigns expires header using map defined in /etc/nginx/conf.d/expires_map.conf expires $expires; |
This works well but may not be possible on a shared server where the global nginx configuration can not be changed.