Nginx Reverse Proxy : post_action if proxy cache hit – Possbile

cachenginxreverse-proxy

We have recently found out about nginxes post_action.

We were wondering it there was a way to use this directive if a proxy cache hit is made?

The flow we were hoping on is as follows:

1) User request comes in
2) If cache HIT goto A / If cache MISS goto B

A) 1) Serve Cached Result
A) 2) post_action to another url on the backend

B) 1) Server request from backend
B) 2) Store result from backend

Any ideas if this is possible via post_action or any other method?

The reasoning behind this is as follows:

We would in essence like to modify the users session (php, but the same concept can apply to most server side languages) while displaying cached content. This would greatly increase the number of cache-able requests we process since these requests only WRITE to sessions, not read from them.

Thanks!

Best Answer

If you didn't solve it yet here is example config that pass your requirements:

server {
    listen 80;
    server_name img1.example;
    root /var/www/images;
    location / { // Users request comes in
        try_files $uri @proxy; // If cache HIT goto A (show) / If cache MISS goto B (@proxy), server cached result
        post_action /url.php; // post_action to another url on the backend
    }

    location @proxy {
        proxy_pass http://static.exmaple; // Server request from backend
        proxy_store /var/www/images$uri; // Store result from backend (cache)
    }
}

Related Topic