ganzuelo
By:
ganzuelo

Leverage browser caching for Nginx

July 28, 2014 56.6k views

I am trying to speed up my Wordpress site. On GTmetrix I am receiving a E(50) for Leverage browser caching.

I am trying to add the code below to my /etc/nginx/nginx.conf file

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }location ~*  \.(pdf)$ {
        expires 30d;
}

Unfortunately any attempt at restarting nginx ends in a [fail]

Please help!

3 comments
3 Answers

You should add that inside of an existing server block. Usually that will be in /etc/nginx/sites-enabled/default

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

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

    location ~*  \.(pdf)$ {
        expires 30d;
    }
}
  • Thanks that did pass with no issues.

    I've been editing /etc/nginx/nginx.conf

    should all that info reflect whats at my /etc/nginx/sites-enabled/default

    also I ran my site through the metrix again but its not showing an improvement

  • hi,

    so, i have to place a new nginx.conf to folder /etc/nginx/sites-enabled/ ?

    /etc/nginx/sites-enabled/default <== this is a directory to /etc/nginx/sites-available/default

    what is this mean?

  • i do not have a file "default" inside sites-enabled directory, instead i am having it inside sites-available. so should i place it inside sites-available/default ???

if you have more than one website create its xyz.conf file and then add this part
/etc/nginx/sites-available/xyz.conf or /etc/nginx/conf.d/xyz.conf or wherever your path is

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

location ~*  \.(pdf)$ {
    expires 30d;
}

different .conf file will help you to reduce the confusion

Have another answer? Share your knowledge.