When you update your website’s static files like JS code, CSS, or images, you will need NGINX server to serve new files. But sometimes, you will find that NGINX continues to serve old files even after you have updated them. This is mainly because NGINX continues to serve the cached versions of those static files. In such cases, you may need to force NGINX to serve new static files. In this article, we will look at the different ways to do this.
How to Force NGINX to Serve New Static Filles
Here are the steps to force NGINX to serve new static files.
1. Manually Clear NGINX cache
NGINX cache is typically located at /var/cache/nginx. Clear its contents with the following command, and NGINX will immediately start serving new files. No need to restart NGINX server in this case.
$ sudo rm -rf /var/cache/nginx
2. Restart NGINX Server
Just restarting NGINX server will automatically clear its cache, and it will start serving new static files.
$ sudo service nginx restart
3. Disable Caching for Static files
You may also disable caching for static files so that NGINX always serves their latest versions, without caching them. For this, you will need to modify NGINX configuration.
Open NGINX configuration file in a text editor.
$ sudo vi /etc/nginx/nginx.conf
Find the location block for static files and add the following line in it to disable caching for those files.
add_header Cache-Control no-cache;
OR
expires -1;
Here is an example of location block for static files, with caching disabled.
location /static/ { root /var/www/html; add_header Cache-Control no-cache; }
Restart NGINX server to apply changes to NGINX configuration file.
That’s it. Basically, you need to either disable cache for static files or delete existing cache. This will force NGINX to serve new static files.
Also read:
How to Return Value from Shell Script Function
How to Retrieve POST data in Django
How to Temporarily Disable Foreign Key Check in MySQL
How to Run Python Script in Django Shell
How to Convert PDF to Image/JPG in Linux
Related posts:
![](https://fedingo.com/wp-content/uploads/2024/05/db908c05ad732bd197c1eaad1c6675d2.jpeg)
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.