# How to Clear the Cache of Nginx?

Clearing the cache in Nginx depends on the type of caching you have configured. Nginx can cache content in various ways, including using its built-in caching mechanisms (like proxy caching or FastCGI caching) or through third-party modules. Here’s a guide to clearing different types of caches in Nginx:

### **1. Clearing Proxy Cache**

If you are using Nginx’s proxy caching, you can clear the cache by deleting the cached files manually.

### **Steps:**

1. **Locate the Cache Directory:**
    - The cache directory is defined in the `proxy_cache_path` directive in your Nginx configuration file.
        
        ```
        proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
        ```
        
        In this example, the cache directory is `/var/cache/nginx`.
        
2. **Delete Cached Files:**
    - Remove the cached files from the directory.
        
        ```bash
        sudo rm -rf /var/cache/nginx/*
        ```
        
3. **Reload Nginx:**
    - After clearing the cache, reload Nginx to apply the changes.
        
        ```bash
        sudo systemctl reload nginx
        ```
        

### **2. Clearing FastCGI Cache**

If you’re using Nginx’s FastCGI caching, you can also clear the cache by deleting the cached files.

### **Steps:**

1. **Locate the Cache Directory:**
    - The cache directory for FastCGI is specified in the `fastcgi_cache_path` directive.
        
        ```
        fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m max_size=1g inactive=60m;
        ```
        
        In this example, the cache directory is `/var/cache/nginx/fastcgi`.
        
2. **Delete Cached Files:**
    - Remove the cached files from the directory.
        
        ```bash
        sudo rm -rf /var/cache/nginx/fastcgi/*
        ```
        
3. **Reload Nginx:**
    - After clearing the cache, reload Nginx.
        
        ```bash
        sudo systemctl reload nginx
        ```
        

### **3. Clearing Cache for Specific URLs**

If you want to clear the cache for specific URLs rather than the entire cache, you can use the `proxy_cache_purge` directive if configured.

### **Steps:**

1. **Enable Cache Purge Module:**
    - Ensure the `ngx_cache_purge` module is installed and enabled in your Nginx configuration.
2. **Configure Cache Purge Location:**
    - Add a `location` block to handle cache purging.
        
        ```
        location /purge/ {
            allow 127.0.0.1;  # Allow only localhost to purge cache
            deny all;
            proxy_cache_purge my_cache $scheme$proxy_host$request_uri;
        }
        ```
        
3. **Purge Cache via URL:**
    - Use a tool like `curl` to send a purge request.
        
        ```bash
        curl -X PURGE <http://localhost/purge/your-cached-url>
        ```
        

### **4. Clearing Cache for Static Files**

If you cache static files, you may need to clear the cache by deleting the cached static files.

### **Steps:**

1. **Locate Static Cache Directory:**
    - Identify where your static files are cached.
2. **Delete Cached Files:**
    - Remove cached static files as needed.
        
        ```bash
        sudo rm -rf /path/to/static/cache/*
        ```
        

### **5. Reload or Restart Nginx**

After clearing the cache, it's generally a good practice to reload or restart Nginx to ensure the changes take effect.

- **Reload Nginx:**
    
    ```bash
    sudo systemctl reload nginx
    ```
    
- **Restart Nginx:**
    
    ```bash
    sudo systemctl restart nginx
    ```
    

### **Summary**

- **Proxy Cache**: Clear by deleting files in the cache directory specified in `proxy_cache_path`.
- **FastCGI Cache**: Clear by deleting files in the cache directory specified in `fastcgi_cache_path`.
- **Specific URL Cache**: Use the `proxy_cache_purge` directive if configured.
- **Static File Cache**: Remove cached static files from the specified directory.

By following these steps, you can effectively clear the cache in Nginx to ensure that outdated or incorrect cached content is removed.