dib258's avatar
Level 11

Adding expiration date on static files (Nginx configuration)

Hi,

I'm trying to optimize a client's website on laravel and using Google's page speed, it keeps saying that I send no expiration date on the static files I send to the user.

So I tried to search how can I do that I found that I need to add some code in the nginx.conf. (https://www.howtoforge.com/make-browsers-cache-static-files-on-nginx)

So I went to the nginx.conf on my server and tried to add the following code

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
   expires 4w;
} 

They say I can add it inside the http{} or the server{}. Since there's no server{} in my nginx.conf file I tried to add it in the http one.

I also found a way to check if the file has really an expiration date with this command

curl -I http://www.mysite.com/picture.jpg

and I get this response

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 22 Dec 2015 21:00:49 GMT
Content-Type: image/png
Content-Length: 7595
Last-Modified: Tue, 22 Dec 2015 18:07:53 GMT
Connection: keep-alive
ETag: "56799179-1dab"
Accept-Ranges: bytes

So no expiration date on the file. So I tried to change the place and created a server {}. (I also restarted each time nginx with sudo service nginx reload off course !) And I get the same answer no expiraction date on the file.

Am I doing something wrong, using the wrong code or adding the code at the wrong place, to tell nginx that I want him to send an expiration date !

Thanks in advance !

0 likes
11 replies
dib258's avatar
dib258
OP
Best Answer
Level 11

I finally found where the code must go. Instead of putting the code in /etc/nginx/nginx.conf, you must go to /etc/nginx/sites-available/{ your server file } and add it in the server {}.

I also optimized the code to be more large (including fonts for example).

location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
}
bashy's avatar

Just a clean up of your location block

# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png|woff|ttf|otf|svg|woff2|eot)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires 30d; # or use max
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
5 likes
alexmansour's avatar

@bashy after trying your location block I'm getting the following, when running 'sudo nginx -c /etc/nginx/nginx.conf -t'

nginx: [emerg] unknown directive "//" in /etc/nginx/sites-enabled/domain.com:63 nginx: configuration file /etc/nginx/nginx.conf test failed

Thoughts?

tmalkiew's avatar

remove entire "// or use max" it's just a comment

stayallive's avatar

@mayasl this was a long time ago but for people coming across this and also doing the same (putting the block in the nginx.conf directly).

If you do that nginx takes the wrong root for the location block and thus cannot serve the files, this must be placed in your vhost server config file or as an include in your vhost server file if you don't want to copy paste it.

audetcameron@gmail.com's avatar

For anyone interested. W3TC provides a block configuration for caching in NGINX that can most likely be used in Laravel as well. (place inside your location / block

gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel;
location ~ \.(css|htc|less|js|js2|js3|js4)$ {
 expires 31536000s;
 etag on;
 if_modified_since exact;
 try_files $uri $uri/ /index.php?$args;
}
location ~ \.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$ {
 etag on;
 if_modified_since exact;
 try_files $uri $uri/ /index.php?$args;
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|_ttf|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
 expires 31536000s;
 etag on;
 if_modified_since exact;
 try_files $uri $uri/ /index.php?$args;
}
add_header Referrer-Policy "no-referrer-when-downgrade";

Please or to participate in this conversation.