Welcome! Log In Create A New Profile


Set `expires` by MIME type

September 03, 2018 04:13PM
I am attempting to use `expires` on Nginx 1.15.3 to define the expiry of files on a per MIME type basis.

I have used [1] as a base, and constructed the following `map` in the `http` section of a `include`-d `server` block (domain sanitised):

map $sent_http_content_type $www_example_com_expires {
default 1M;
application/atom+xml 1h;
application/javascript 1M;
application/json 0s;
application/ld+json 0s;
application/manifest+json 1w;
application/rdf+xml 1h;
application/rss+xml 1h;
application/schema+json 0s;
application/x-javascript 1M;
application/xml 0s;
font/woff 1M;
image/gif 1M;
image/jpeg 1M;
image/png 1M;
image/svg+xml 1M;
image/vnd.microsoft.icon 1M;
image/webp 1M;
image/x-icon 1M;
text/cache-manifest 0s;
text/css 1M;
text/html 0s;
text/javascript 1M;
text/x-cross-domain-policy 1w;
text/xml 0s;
video/mp4 1M;
video/webm 1M;

Later on, after the `map` is defined, I call it using `expires` in a `server` block:

server {#IPv4 and IPv6, https, PHP fastcgi, check https://cipherli.st for latest ciphers
access_log /var/log/nginx/www.example.com.access.log ipscrubbed;
add_header Access-Control-Allow-Origin "https://*.example.com";
add_header Content-Security-Policy "default-src 'self'; connect-src 'self' https://api.github.com; font-src 'self'; img-src 'self' data: * https://*; media-src 'self' * https://*; style-src 'self' 'unsafe-in$
add_header Expect-CT "max-age=0; report-uri=https://example.com/expect-ct-report";
add_header Feature-Policy "camera 'self'; geolocation 'none'; microphone 'none'; midi 'none'; payment 'none'";
add_header Referrer-Policy strict-origin;
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
error_log /var/log/nginx/www.example.com.error.log crit;
etag off;
expires $www_example_com_expires;
index index.html index.php;
listen [::]:443 http2 ssl;
listen 443 http2 ssl;

My config passes the `nginx -t` self-test with no errors, and I can restart Nginx without issue.

In the browser inspector, all MIME types are assigned a 1 month expiry, as if they're inheriting the `default` value from the map. Example headers for a .php file:

Date: Mon, 03 Sep 2018 20:09:30 GMT
Expires: Wed, 03 Oct 2018 20:09:30 GMT

If I remove the `expires` directive, the 'Expires:' header is not shown, so `expires` is doing *something*.

I suspect my syntax is wrong, and I would be very grateful for any feedback -- I am particularly interested a clue or pointer to aid my research into why this is not working.

Thank you for your attention and interest.

[1] http://nginx.org/en/docs/http/ngx_http_headers_module.html#expires
Subject Author Posted

Set `expires` by MIME type

petecooper September 03, 2018 04:13PM

Re: Set `expires` by MIME type

Richard Stanway September 04, 2018 07:10AM

Re: Set `expires` by MIME type

Francis Daly September 04, 2018 08:22AM

Re: Set `expires` by MIME type

petecooper September 04, 2018 09:41AM

Re: Set `expires` by MIME type

Francis Daly September 04, 2018 11:28AM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 65
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready