Cannot activate browser caching

I don't know what I'm doing wrong but none of the changes I am making to improve performance using hummingbird is having any effect. For example, Hummingbird is telling me to enable browser caching but I have added the recommended settings to my nginx.conf (though not above the http section as you are recommending because the no location code is allowed there but rather in a new server section in /etc/nginx/sites-available/default) but when I re-run the performance test with hummingbird, it is still telling me the same thing.

And: no, I have no caching plugin installed.

  • Christoph

    Here is an excerpt from my /etc/nginx/sites-available/default (with my domain name anonymized)
    I tried adding the section in question directly to the nginx.conf but it wouldn't let me (it said no location directives allowed here, or something like that). The instructions in Hummingbird were rather weak (if not plain wrong), I have to say...

    upstream php {
            server unix:/tmp/php-cgi.socket;
            server 127.0.0.1:9000;
    }
    
    server {
            listen 80 default_server;
            listen [::]:80 default_server;
            server_name mydomain.com *.mydomain.com 46.xx.xx.xx;
            return 301 https://$server_name$request_uri;
            }
    
    # recommendation from hummingbird plugin
    server {
            location ~* \.(txt|xml|js)$ {
                expires 1h;
            }
    
            location ~* \.(css)$ {
                expires 1h;
            }
    
            location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
                expires 1h;
            }
    
            location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
               expires 1h;
            }
    }

    Yes, I reloaded NGINX many times.

    The reason I set the expiry time so low is because I am currently making various changes while experimenting, so I didn't want the cache to interfere too much with than.

  • Christoph

    So you want me to move the server directive that I posted above below "# recommendation from hummingbird plugin" into /etc/nginx/nginx.conf? I'm not sure what the difference would be, since, as you know, the the /etc/nginx/sites-available/default is by default included in the nginx.conf via the include command.

    But I did it and here is the error it gives me

    $ sudo nginx -t
    nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:120
    nginx: configuration file /etc/nginx/nginx.conf test failed

    If you are wondering what's at line 120, it is the server directive I just added.

  • Christoph

    Here you go:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            client_max_body_size 32m;
    
            ##
            # SSL Settings
            ##
    
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
    # Enable Gzip compression
    gzip          on;
    
    # Compression level (1-9)
    gzip_comp_level     5;
    
    # Don't compress anything under 256 bytes
    gzip_min_length     256;
    
    # Compress output of these MIME-types
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-javascript
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/svg+xml
        image/x-icon
        text/css
        text/plain
        text/javascript
        text/x-component;
    
        # Disable gzip for bad browsers
        gzip_disable  "MSIE [1-6]\.(?!.*SV1)";
    
            ##
            # Virtual Host Configs
            ##
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    
    # recommendation from hummingbird plugin
    server {
            location ~* \.(txt|xml|js)$ {
                expires 1h;
            }
    
            location ~* \.(css)$ {
                expires 1h;
            }
    
            location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
                expires 1h;
            }
    
            location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
               expires 1h;
            }
    }
  • Kasia Swiderska

    Hello Christoph,

    Can you move server block inside http block? Please see here https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts

    The Server Context

    The "server" context is declared within the "http" context. This is our first example of nested, bracketed contexts. It is also the first context that allows for multiple declarations.

    Let me know if this works for you.

    kind regards,
    Kasia

  • Christoph

    I changed the nginx.conf as advised and restarted nginx but hummingbird still says that browser caching is disabled:

    My nginx.con now looks like this:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            client_max_body_size 32m;
    
            ##
            # SSL Settings
            ##
    
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
    # Enable Gzip compression
    gzip          on;
    
    # Compression level (1-9)
    gzip_comp_level     5;
    
    # Don't compress anything under 256 bytes
    gzip_min_length     256;
    
    # Compress output of these MIME-types
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-javascript
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/svg+xml
        image/x-icon
        text/css
        text/plain
        text/javascript
        text/x-component;
    
        # Disable gzip for bad browsers
        gzip_disable  "MSIE [1-6]\.(?!.*SV1)";
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    
            ## recommendation from hummingbird plugin
            server {
            location ~* \.(txt|xml|js)$ {
                expires 1h;
            }
    
            location ~* \.(css)$ {
                expires 1h;
            }
    
            location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm$
                expires 1h;
            }
    
            location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
               expires 1h;
            }
            }
    }
  • Nithin

    Hey Christoph,

    Hope you are doing good today. :slight_smile:

    There is an opening bracket missing in the above shared code, under:

    location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm$

    This should be how the structure for adding browser caching should be:

    http {
        server {
            location...
        }
        [...]
    }

    Could you please try adding the following, and check whether it works fine:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            client_max_body_size 32m;
    
            ##
            # SSL Settings
            ##
    
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
    # Enable Gzip compression
    gzip          on;
    
    # Compression level (1-9)
    gzip_comp_level     5;
    
    # Don't compress anything under 256 bytes
    gzip_min_length     256;
    
    # Compress output of these MIME-types
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/rss+xml
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-javascript
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/svg+xml
        image/x-icon
        text/css
        text/plain
        text/javascript
        text/x-component;
    
        # Disable gzip for bad browsers
        gzip_disable  "MSIE [1-6]\.(?!.*SV1)";
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    
            ## recommendation from hummingbird plugin
            server {
    
    		location ~* \.(txt|xml|js)$ {
        			expires 8d;
    		}
    
    		location ~* \.(css)$ {
    		    expires 8d;
    		}
    
    		location ~* \.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac)$ {
        			expires 8d;
    		}
    
    		location ~* \.(jpg|jpeg|png|gif|swf|webp)$ {
    		    expires 8d;
    		}
    
            }
    }

    Please edit the expires duration according to your requirements, and please let us know how the above code works. Please make sure to click the Re-Check Expiry button, once you have added the code. I hope this helps. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.