openresty / srcache-nginx-module

Transparent subrequest-based caching layout for arbitrary nginx locations.
http://wiki.nginx.org/NginxHttpSRCacheModule
476 stars 105 forks source link

X-Cached header does not appear in the response for 404 status code #77

Closed supertwister closed 5 years ago

supertwister commented 5 years ago

I want that Redis will store also 404 errors. I have added the following directives to my nginx configuration file: srcache_store_statuses 200 301 302 404;

For 200 status code for example, I can see X-Cached header in the response (HIT, BAYPASS, etc). However, I cannot see this header for 404 status code. I confirm that according to Redis logs, It seems that the configuration works properly (I can see PUT and GET calls for 404 error).

What could be the issue?

Here is the server.conf file:

# admin web #
server {
    listen 80;
    server_name  ~^(.+)$;

    root   /var/www/links/x.y.co.il;
    index  index.php index.html index.htm;

    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;

    location / {
        error_page 420 = @queryone;

        set $key $http_host$request_uri;
        set_escape_uri $escaped_key $key;

        set_md5 $redis_header $key;
        add_header X-SRKey "$redis_header";

        srcache_fetch GET /redis $key;
        add_header X-Cached $srcache_fetch_status;
        try_files $uri $uri/ /index.php?$args;
    }

    location @queryone {
        add_header X-Cached $srcache_fetch_status;
        try_files $uri $uri/ /index.php?$args;
    }

    ## redis read ##
    location = /redis {
        internal;
        set_md5 $redis_key $args;
        redis_connect_timeout 1s;
        redis_pass redis:6379;
    }

    ## redis store ##
    location = /redis2 {
        internal;

        set_unescape_uri $exptime $arg_exptime;
        set_unescape_uri $key $arg_key;
        set_md5 $key;

        redis2_query set $key $echo_request_body;
        redis2_query expire $key $exptime;
        redis2_connect_timeout 1s;
        redis2_pass redis:6379;
    }

    location ~ \.php$ {
        add_header X-Cached $srcache_fetch_status;
        srcache_store_statuses 200 301 302 404;
        srcache_store PUT /redis2 key=$escaped_key&exptime=60;
        try_files $uri =404;
        include         /etc/nginx/fastcgi.conf;
        fastcgi_pass    php:9000;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }

    include /etc/nginx/default.d/*.conf;
}