Score:0

getting 412 precondition failed with ngx_cache_purge when trying to purge cache

lu flag

I am new to this and just started using nginx for my pet projects. now i would like to install and use ngx_cache_purge module from https://github.com/nginx-modules/ngx_cache_purge, since it has some cool integrations with wordpress.

here is the docker image i came up with:

FROM nginx:1.18.0

RUN apt update && apt install -y --no-install-recommends  --no-install-suggests \
            wget gcc make zlib1g-dev libpcre3-dev

RUN mkdir -p /home/nginx && cd /home/nginx && \
    wget -qO - https://nginx.org/download/nginx-1.18.0.tar.gz | tar zxfv - && \
    wget -qO - https://github.com/nginx-modules/ngx_cache_purge/archive/refs/tags/2.5.1.tar.gz | tar zxfv -

RUN cd /home/nginx/nginx-1.18.0 && \
    ./configure --add-dynamic-module=../ngx_cache_purge-2.5.1 --with-compat && \
    make modules && \
    cp objs/ngx_http_cache_purge_module.so /usr/lib/nginx/modules/ 

here is a sample nginx config:

load_module modules/ngx_http_cache_purge_module.so;

http {
    gzip off;
    gzip_vary off;
    gzip_proxied off;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 use_temp_path=off keys_zone=proxycache:256m
    inactive=30d
    max_size=4g;

    proxy_cache_purge PURGE from 127.0.0.1;

    server {
        listen *:8008;
        location ~* /purgea(/.*) {
            add_header X-url-match $scheme${request_method}my_proxy_server$1$args always;
            proxy_cache_purge proxycache $scheme${request_method}my_proxy_server$1$args;
            cache_purge_response_type json;
        }
    }
}

here is confirmation that cache file exists:

root@a65be2e6f69c:/# grep -R -a -E "^KEY:" /var/cache/nginx/proxy_cache/ | grep "jquery.min.js?ver=3.6.0"
/var/cache/nginx/proxy_cache/b/3c/e0ec9b13973d56a5780010cadccc73cb:KEY: httpGETmy_proxy_server/wp-includes/js/jquery/jquery.min.js?ver=3.6.0
root@a65be2e6f69c:/# 

here is the request i am making to purge the cache for that uri:

root@a65be2e6f69c:/# curl -v localhost:8008/purgea/wp-includes/js/jquery/jquery.min.js?ver=3.6.0
> GET /purgea/wp-includes/js/jquery/jquery.min.js?ver=3.6.0 HTTP/1.1
> Host: localhost:8008
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 412 Precondition Failed
< Server: nginx
< Date: Fri, 13 May 2022 01:04:17 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 166
< Connection: keep-alive
< X-url-match: httpGETmy_proxy_server/wp-includes/js/jquery/jquery.min.jsver=3.6.0
< 
<html>
<head><title>412 Precondition Failed</title></head>
<body>
<center><h1>412 Precondition Failed</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host localhost left intact
root@a65be2e6f69c:/#
cn flag
did you ever figure this out?
lu flag
@deweydb i've went to the github of the module as well, and one of the developers answered with a solution. now i never tried it myself as i moved to a different project, but you can try it: https://github.com/nginx-modules/ngx_cache_purge/issues/38#issuecomment-1144193046. please let me know if this worked.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.