disable nginx cache

How To Disable NGINX Cache

NGINX is a powerful web server that offers many features including caching of website content. However, if NGINX cache doesn’t work properly, you need to disable NGINX cache or delete cache temporarily. Here’s how to disable NGINX cache.

 

How To Disable NGINX Cache

Here are the steps to disable NGINX cache. After you disable NGINX cache, you may want to use a reporting software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well and spot issues quickly.

 

1. Open NGINX config file

If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then run the following command

$ sudo vi /etc/nginx/nginx.conf

 

If you have configured separate virtual hosts for your website (e.g www.domain.com), such as /etc/nginx/sites-enabled/domain.conf then open it with the following command

$ sudo vi /etc/nginx/sites-enabled/domain.conf

 

Bonus Read : How to Redirect 404 to URL in NGINX

 

2. Disable NGINX cache

There are multiple ways to disable NGINX cache. If you want to disable NGINX cache for your entire website, add the following lines (in bold) in location / block

server {
    listen 8080;
    server_name localhost;

    location / {
        root /your/site/public;
        index index.html;

        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
    }
}

In the above code, the lines in bold disable NGINX cache. The add_header directive will modify the response headers, thereby disabling further caching.

You can also use expires directive to disable cache, as shown below.

location / {
        root /your/site/public;
        index index.html;

        # kill cache
        expires -1;
    }

If you only want to disable NGINX cache for specific file types like jpg,pdf,etc you can include those file extensions, in location directive

location ~* \.(jpg|pdf|jpeg)$ {
    expires -1;
}

 

Bonus Read : How to Redirect URL in NGINX

 

You can also disable caching using proxy_no_cache directive

location / {
    # don't cache it
    proxy_no_cache 1;
    # even if cached, don't try to use it
    proxy_cache_bypass 1; 
}

If you only want to disable cache in a specific folder (e.g /product)

location /product {
    # don't cache it
    proxy_no_cache 1;
    # even if cached, don't try to use it
    proxy_cache_bypass 1; 
}

 

Bonus Read : How To Implement NGINX Reverse Proxy

 

Delete NGINX cache

If you only want to delete NGINX cache and not disable it, then clear the contents of /var/cache/nginx where NGINX stores its cache contents. This will clear NGINX cache.

$ sudo rm -rf /var/cache/nginx

 

3. Check Syntax and Restart NGINX

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

 

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

 

That’s it! Now your NGINX server will not cache your website contents, as per your requirements.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!