sonerezh icon indicating copy to clipboard operation
sonerezh copied to clipboard

Optimization: enable caching for albums covers

Open MightyCreak opened this issue 9 years ago • 8 comments

The covers are fetched all the time but I think they could reasonably be cached since they should not change that often and it consumes both bandwitdth, cpu and loading time.

This image shows what happens when refreshing the Albums page (it's only a portion of the complete loading):

capture d ecran 2016-12-21 a 00 58 37

I tried to add a special location directive in my nginx configuration file, but the suffixes like [email protected] at the end of the URL make it so that the file doesn't exist. It seems that every URL of that kind uses the try_files fallback rule: /index.php?$args.

MightyCreak avatar Dec 21 '16 01:12 MightyCreak

I tried to add these lines before $this->response->file($resized); in app/Controller/ImgController.php:

        $this->response->header('Expires', gmdate('r', time() + (60 * 60 * 24 * 14)));
        $this->response->header('Cache-Control', 'public');

I can see the header properties with curl -I but I keep on having the 200 HTTP status in Firefox...

MightyCreak avatar Dec 21 '16 02:12 MightyCreak

This might also help to have less silence between two songs (see https://github.com/Sonerezh/sonerezh/issues/242#issuecomment-268394043)

MightyCreak avatar Dec 21 '16 06:12 MightyCreak

This kind of cache should be managed by your web server (Apache, Nginx...) and not by Sonerezh itself. Here is the same screenshot, from the online demo:

outils de developpement - issues sonerezh-sonerezh - https --github com-sonerezh-sonerezh-issues_029

As you can see the browser cache works properly. We use the following configuration:

server {
    [...]

    # Serve static images from resized folder
    location ~* \/([^\/]+_[0-9]+x[0-9]+\.[a-z]+) {
        try_files /img/resized/$1 /index.php?$args;
        expires 7d;
        add_header Cache-Control "public";
    }

    [...]
}

It's up to you to edit it to fill your needs :)

lGuillaume124 avatar Dec 21 '16 09:12 lGuillaume124

That's weird... I added you're conf and it still doesn't work.

MightyCreak avatar Dec 21 '16 15:12 MightyCreak

I really don't see... I know it's not really a problem with sonerezh code, but I can't figure out why it doesn't work on my server.

The only thing I know is that if I add add_header X-uri "$fastcgi_script_name"; in the php block, I get X-uri: /index.php when I try to access an image. Which seems understandable since each image URL are sent to the php block...

Here's my nginx conf:

# Enforce https.
server {
    server_name sonerezhisawesome.com;

    listen 80;
    listen [::]:80;
    return 301 https://$server_name$request_uri;
}

server {
    server_name sonerezhisawesome.com;

    listen 443 ssl spdy;
    listen [::]:443 ssl spdy;

    # SSL
    include snippets/ssl-sonerezhisawesome.com.conf;

    access_log /var/log/nginx/sonerezhisawesome.com_access.log;
    error_log /var/log/nginx/sonerezhisawesome.com_error.log;

    root /var/www/sonerezh/app/webroot;
    index index.php;

    # Serve static images from resized folder
    location ~* \/([^\/]+_[0-9]+x[0-9]+\.[a-z]+) {
        try_files /img/resized/$1 /index.php?$args;
        expires 7d;
        add_header Cache-Control "public";
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        expires 14d;
        add_header Cache-Control 'public';
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        #add_header X-uri "$fastcgi_script_name";
    }
}

MightyCreak avatar Dec 22 '16 04:12 MightyCreak

YES! Found it! The regexp is wrong, it doesn't catch the retina case. Here is is the working version I have tested on my server (couldn't test on non retina display, but it should work as well):

    # Serve static images from resized folder
    location ~* /([^/]+_[0-9]+x[0-9]+(@[0-9]+x)?\.[a-z]+)$ {
        try_files /img/resized/$1 /index.php?$args;
        expires 14d;
        add_header Cache-Control "public";
    }

I also removed the \ before the /, they're not needed in here.

BTW I noticed that the static image block is only in the second example in https://www.sonerezh.bzh/docs/en/annexes.html#nginx-server-block-example, I think it should either in both examples or in a special section like "Fine tuning you nginx conf".

MightyCreak avatar Dec 22 '16 06:12 MightyCreak

Nice one @MightyCreak ! We're going to update our doc.

atomiix avatar Dec 22 '16 14:12 atomiix

Do you want me to do it? Just point me to the right folder and I'll update it!

MightyCreak avatar Dec 22 '16 14:12 MightyCreak