Technology Encyclopedia Home >How to configure caching and static content compression on Nginx?

How to configure caching and static content compression on Nginx?

To configure caching and static content compression on Nginx, you can follow these steps:

Caching Configuration

  1. Enable Caching: First, you need to enable caching by defining a cache zone in your Nginx configuration file (nginx.conf or a site-specific configuration file).

    http {
        proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
    }
    
    • proxy_cache_path: Defines the path where cached data will be stored.
    • levels: Defines the directory structure for storing cache files.
    • keys_zone: Defines a named cache zone with a specified size.
    • max_size: Sets the maximum size of the cache.
    • inactive: Specifies how long an item can remain in the cache without being accessed.
    • use_temp_path: Disables the use of a temporary path for caching.
  2. Apply Caching to Locations: Apply the caching to specific locations in your server block.

    server {
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            add_header X-Proxy-Cache $upstream_cache_status;
        }
    }
    
    • proxy_cache: Specifies the cache zone to use.
    • proxy_cache_valid: Defines how long cached responses are considered fresh.
    • add_header: Adds a header to indicate whether the response was served from the cache.

Static Content Compression

  1. Enable Gzip Compression: Enable gzip compression to reduce the size of static content.

    http {
        gzip on;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_min_length 1000;
    }
    
    • gzip on: Enables gzip compression.
    • gzip_types: Specifies the MIME types to compress.
    • gzip_min_length: Sets the minimum length of a response that will be compressed.
  2. Optimize Compression: You can further optimize compression settings.

    http {
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_proxied any;
    }
    
    • gzip_comp_level: Sets the compression level (1-9).
    • gzip_buffers: Specifies the number and size of buffers used for compression.
    • gzip_http_version: Specifies the HTTP version for which gzip compression is enabled.
    • gzip_proxied: Specifies which requests to compress.

Example Configuration

Here is a combined example of caching and static content compression configuration:

http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_proxied any;

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            add_header X-Proxy-Cache $upstream_cache_status;
        }

        location /static/ {
            alias /var/www/static/;
            expires 30d;
            add_header Cache-Control "public";
        }
    }
}

Recommendation for Cloud Services

For deploying and managing Nginx configurations in a scalable and reliable manner, consider using Tencent Cloud's Cloud Load Balancer (CLB) service. CLB can help distribute traffic across multiple instances and supports advanced configurations for caching and compression, ensuring optimal performance for your web applications.