简体   繁体   中英

Nginx Static content caching proxy_cache_bypass proxy_no_cache

I have a problem with using nginx as a load balancer. I could configure it to work as a load balancer but I don't how to make it cache static contents from the proxied servers in the backend such as html,css,js, etc... This means I want nginx to weather to cache or not based on the content of the response from the backend servers if it changed to bypass cache and send requests to the backend and if not to serve from cache. I tried and seached a lot in the internet to make it using many directives such as proxy_cache_bypass and proxy_no_cache but I couldn't. Is there any means to do this if anyone has experience in such topic. these are the configurations:

upstream backend {
    server www.webserver1.com:443 max_fails=3 fail_timeout=15s;
    server www.webserver2.com:443 max_fails=3 fail_timeout=15s;
}

server {
    listen      443 ssl;

    rewrite_log on;
    error_log   /var/log/nginx/lb.error.log;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Proxy-Cache $upstream_cache_status;
    ssl_certificate         /etc/nginx/client.crt;
    ssl_certificate_key     /etc/nginx/client.key;
    ssl on;

    location / {
        proxy_cache backcache;
        #proxy_cache_methods GET HEAD POST;
        #proxy_cache_bypass $cookie_nocache $arg_nocache;
        #proxy_no_cache $cookie_nocache $arg_nocache;
        proxy_cache_min_uses 1;
        #proxy_cache_revalidate on;
        #proxy_cache_valid 200 4m;
        proxy_cache_lock on;
        proxy_cache_background_update on;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_pass https://backend;
    }

}

server {
    listen 80 ;
    if ($http_x_forwarded_proto != 'https') {
        rewrite ^(.*) https://$host$1 redirect;
    }
}

these are the contents of a config. file under /etc/nginx/conf.d/ which is included in the main config. file which is /etc/nginx/nginx.conf and also those 2 lines are in the main config. file:

    proxy_cache_path /var/lib/nginx/cache keys_zone=backcache:20m max_size=100m;
    proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args$cookie_user";

Your backend servers could be the root cause of that problem, if those servers were improperly configured. For example sending Cache-Control headers on requests to static files.

According to that docs by default, NGINX respects the Cache-Control headers from origin servers. It does not cache responses with Cache-Control set to Private, No-Cache, or No-Store or with Set-Cookie in the response header.

You can permanently change this behavior by adding those directives:

proxy_ignore_headers Cache-Control;
proxy_cache_valid any 30m; 

So the config will look like:

    location / {
        proxy_cache backcache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_valid 200 302 10m;;
        proxy_cache_lock on;
        proxy_cache_background_update on;
        proxy_ignore_headers Cache-Control;
        proxy_cache_valid any 30m;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_pass https://backend;
    }

Hope it will help you to figure out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM