Nginx 如何不缓存html

4,119 阅读1分钟

主页html缓存304, 想要每次打开都获取最新的index.html

需要配置Nginx不缓存html

if ($request_filename ~* ^.*?.(html|htm)$) {
    add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}

需要添加到 location

完整的代码

server {
    listen    8080 so_keepalive=on;
    server_name   localhost;

    access_log  logs/nginx.access.log  main;

    if ($host !~ "\.xxx\.com$|\.xxx\.com\:|\.xxx\.com\.cn$|\.xxx\.com\.cn:|\.xxx\.cn$|\.xxx\.cn\:|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:")
    {
       rewrite
       ^/(.*)$ https://error.xxx.com/errorname.html?url=%1&fr=BDIM redirect;
    }

    location ^~ /WEB-INF{
        deny all;
    }

    location ~* /bin/ {
        return 403;
    }

    location ~* \.(sql|bak|inc|old)$ {
        return 403;
    }

    location / {
        root   /home/work/project;
        index  index.html;
        if ($request_filename ~* ^.*?.(html|htm)$) {
            add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        }
    }

    error_page 400 401 403 404 500 501 502 503 504 505      https://www.xxxx.com/search/error.html;
}