•  · 38 friends

SOLVED:Browser caching is not working properly with nginx or apache

Hello UNA team,

I tried with both servers but the browsers are downloading the pictures each time.
For my site the timeline downloads 17 MBs each time thats too much!

Even at una.io most of the images are not browser cached.

with apache the conf is:

mod expires is enabled

ExpiresActive on
ExpiresDefault "access plus 3 seconds"
text/html "access plus 60 minutes"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType image/svg+xml "access plus 1 months"
ExpiresByType text/js "access plus 1 months"
ExpiresByType text/css "access plus 1 months"
ExpiresByType text/woff2 "access plus 1 months"
ExpiresByType application/x-shockwave-flash "access plus 30 days"
ExpiresByType text/javascript "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
ExpiresByType application/x-javascript "access plus 30 days"
ExpiresByType application/x-gzip "access plus 30 days"
ExpiresByType image/x-icon "access plus 30 days"
ExpiresByType image/vnd.microsoft.icon "access plus 30 days"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType application/font-sfnt "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"


with nginx the conf is:
 location ~* \.(pdf|jpg|jpeg|gif|css|png|js|ico|svg|woff)$ {
    expires 30d;
 access_log off;
 add_header Cache-Control "public";
  }

0 0 0 0 0 0
  • 1291
Replies (10)
  • By default files which are in UNA storage are already send proper headers to be cached by browser, default setting is 30 days, you can change it in sys_objects_storage table cache_control field, however it's for public files only. Private files are cached for short period of time, the same as token_life, by default 6 minutes.

    Also I believe that more correct setting for Nginx to cache static files would be (if UNA is installed in the domain ROOT folder):

        location ~* ^(/cache_public/|/plugins_public/|/modules/|/studio/|/template/).+\.(jpg|jpeg|gif|css|png|js|ico|svg|eot|ttf|woff|woff2|)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
            try_files       $uri =404;
        }
    
    
    0 0 0 0 0 0
    • As I see now reload F5 reloads all the images with max-age=0 while in normal browsing like clicking the homepage link the images are YES cached.
      So there is no problem in caching.

      I am leaving my previous answer to be a guidance for the members who have the same question.

      Baloo 

      -----------------------------------------------------------------------------------------------------------


      When I checked at UNA site for example this image:
      https://una.io/s/bx_timeline_photos_processed/ay4cvkatczxt52xxnuf7kafexueezwsk.jpg
      it doesn't seem like it is cached.


      And request is:
      GET /s/bx_timeline_photos_processed/ay4cvkatczxt52xxnuf7kafexueezwsk.jpg HTTP/1.1
      Host: una.io
      User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0
      Accept: image/webp,*/*
      Accept-Language: tr,en-US;q=0.7,en;q=0.3
      Accept-Encoding: gzip, deflate, br
      Referer: https://una.io/
      Connection: keep-alive
      Cookie: em_cdn_uid=t%3D1559328973944%26u%3D6d12f6fdc71948ef8c0ca26e3a48b2e7; _ga=GA1.2.2132095043.1559328974; _gid=GA1.2.990492594.1559328974; em_p_uid=l:1559625371170|t:1559328984848|u:a88cf207696c475096db69c0d9616601; memberID=2159; memberPassword=c00f2e3e451b8609ede3205fb4cb67c93427c549; memberSession=tbf%2C6c.R9%3DTpah8BW%26%3FSj4bw%2CwVFDkf3
      Cache-Control: max-age=0

      Response header is:

      HTTP/1.1 200 OK
      Server: nginx
      Date: Tue, 04 Jun 2019 05:39:25 GMT
      Content-Type: image/jpeg
      Content-Length: 81877
      Connection: keep-alive
      Cache-Control: public, must-revalidate, max-age=2592000
      Accept-Ranges: 0-81877
      Content-Disposition: inline; filename="8487665900FB_IMG_1559327359772.jpg"
      Content-Range: bytes 0-81876/81877



      For a cached image like this

      https://una.io/s/sys_images_resized/rhpwgredcuaz2zmaqtxedmtwblqy4auu.png

      there is the difference where Cache-Control: max-age=0 does not exist in the request header.

      It seems like the images like above in timeline, they are not cached also here at una, what shall we do to make them cached? Because our current timeline version is causing a lot of image files to reload each time which takes a lot of time and network cost.


      0 0 0 0 0 0
      • In Gmetrix, we get good scores except on this specific point, it's a shame.

        0 0 0 0 0 0
        • By default files which are in UNA storage are already send proper headers to be cached by browser, default setting is 30 days, you can change it in sys_objects_storage table cache_control field, however it's for public files only. Private files are cached for short period of time, the same as token_life, by default 6 minutes.

          Also I believe that more correct setting for Nginx to cache static files would be (if UNA is installed in the domain ROOT folder):

              location ~* ^(/cache_public/|/plugins_public/|/modules/|/studio/|/template/).+\.(jpg|jpeg|gif|css|png|js|ico|svg|eot|ttf|woff|woff2|)$ {
                  expires 1M;
                  access_log off;
                  add_header Cache-Control "public";
                  try_files       $uri =404;
              }
          
          

          Hi,

          When I used this caching block for nginx, it stopped loading some pictures on the una installation, such as the default user icon (/modules/boonex/persons/template/images/no-picture-thumb.png) - returns 404.

          Is there something I'm missing?

          0 0 0 0 0 0
          •     location ~* ^(/cache_public/|/plugins_public/|/modules/|/studio/|/template/).+\.(jpg|jpeg|gif|css|png|js|ico|svg|eot|ttf|woff|woff2|)$ {
                    expires 1M;
                    access_log off;
                    add_header Cache-Control "public";
                    try_files       $uri =404;
                }
            
            

            When I used this caching block for nginx, it stopped loading some pictures on the una installation, such as the default user icon (/modules/boonex/persons/template/images/no-picture-thumb.png) - returns 404.

            Is there something I'm missing?

            Above rules are for situation when UNA is installed in ROOT folder of your domain, if UNA is installed in subfolder then it need to be changed accordingly.

            0 0 0 0 0 0
            •     location ~* ^(/cache_public/|/plugins_public/|/modules/|/studio/|/template/).+\.(jpg|jpeg|gif|css|png|js|ico|svg|eot|ttf|woff|woff2|)$ {
                      expires 1M;
                      access_log off;
                      add_header Cache-Control "public";
                      try_files       $uri =404;
                  }
              
              

              When I used this caching block for nginx, it stopped loading some pictures on the una installation, such as the default user icon (/modules/boonex/persons/template/images/no-picture-thumb.png) - returns 404.

              Is there something I'm missing?

              Above rules are for situation when UNA is installed in ROOT folder of your domain, if UNA is installed in subfolder then it need to be changed accordingly.

              Understood, but my UNA is installed in the root folder. Something wrong with caching?

              0 0 0 0 0 0
              • Understood, but my UNA is installed in the root folder. Something wrong with caching?

                I would suggest to check Nginx access and error logs to find the reason of the problem.

                0 0 0 0 0 0
                • Understood, but my UNA is installed in the root folder. Something wrong with caching?

                  I would suggest to check Nginx access and error logs to find the reason of the problem.

                  There was no error posted, just a 404 in the access log.

                  If I just change

                  try_files \$uri =404;

                  to

                  try_files $uri =404;

                  It works fine after that.

                  Is that ok?

                  0 0 0 0 0 0
                  • Understood, but my UNA is installed in the root folder. Something wrong with caching?

                    I would suggest to check Nginx access and error logs to find the reason of the problem.

                    There was no error posted, just a 404 in the access log.

                    If I just change

                    try_files \$uri =404;

                    to

                    try_files $uri =404;

                    It works fine after that.

                    Is that ok?

                    I just realized that within this thread, you don't mention putting a "\" in front of "$uri".

                    However, I copied over the code from here, initially, and it is there - that was what was causing the issue.

                    https://una.io/page/view-discussion?id=760

                    All solved now, thanks.

                    0 0 0 0 0 0
                    • I just realized that within this thread, you don't mention putting a "\" in front of "$uri".

                      However, I copied over the code from here, initially, and it is there - that was what was causing the issue.

                      https://una.io/page/view-discussion?id=760

                      Thank you, I've corrected the code there.

                      0 0 0 0 0 0
                      Not logged in users can't 'Comments Post'.
                      2
                      4
                      1
                      4
                      Added:
                      Category:

                      UNA - Social Media Software Framework

                      Close