Skip to content

Examples

Adaptive Content Serving

When serving content, it's often necessary to tailor the experience based on the user's device type. This pattern allows you to serve mobile-optimized pages without redirects.

location / {
    set $variant "desktop";
    if ($is_mobile) { set $variant "mobile"; }
    if ($is_tablet) { set $variant "tablet"; }

    proxy_pass http://backend;
    proxy_set_header X-Device-Variant $variant;
}

Caveat: Ensure your backend can handle the X-Device-Variant header to serve the appropriate content.

Cache Key Variation

To optimize caching strategies, vary the cache key based on the device type. This ensures that cached content is relevant to the user's device.

proxy_cache_key "$scheme$request_uri|$device_type";

# Or just differentiate between mobile and desktop
proxy_cache_key "$scheme$request_uri|$is_mobile";

Caveat: Consider the cache storage impact when varying by detailed device types.

Bot Management

Effectively manage bot traffic by blocking AI training crawlers while allowing legitimate search engines.

location / {
    if ($is_ai_crawler) {
        return 403 "AI crawlers not permitted";
    }
    # Allow search engines and other traffic
    proxy_pass http://backend;
}

Caveat: Ensure that legitimate bots are not mistakenly categorized as AI crawlers.

Analytics Enrichment

Enhance analytics data by passing device context to backend systems. This can provide insights into user behavior across different devices.

location / {
    proxy_pass http://backend;
    proxy_set_header X-Device-Type $device_type;
    proxy_set_header X-Device-Brand $device_brand;
    proxy_set_header X-Device-Model $device_model;
}

Caveat: Ensure backend systems are equipped to process the additional headers.

AI Crawler Control

Identify and manage AI crawlers separately from other bot traffic. This allows for specific handling or blocking of AI crawlers.

location / {
    if ($is_ai_crawler) {
        return 403 "Access denied for AI crawlers";
    }
    proxy_pass http://backend;
}

Caveat: Regularly update detection patterns to keep up with new AI crawlers.

See also

For troubleshooting tips and common issues, refer to troubleshooting.md.