openresty / srcache-nginx-module

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

$srcache_expire is always empty #61

Open onnimonni opened 7 years ago

onnimonni commented 7 years ago

Hey!

I have been trying srcache with redis for few weeks now and today I noticed that the cache keys are not expiring automatically.

I'm using this config in local docker environment:

##
# Adds internal locations for storing and getting full page cache from redis
##

srcache_default_expire 10s;
srcache_max_expire 10s;

location /redis-fetch {
    internal;

    ##
    # In order to use password authentication we use custom redis module which adds $redis_auth:
    # - https://github.com/Yongke/ngx_http_redis-0.3.7
    ##

    # Read the configuration from system envs
    set $redis_auth '';
    set $redis_db 0;

    set $redis_key $args;

    redis_pass 172.17.0.6:6379;
}

location /redis-store {
    internal;

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

    # redis module pipelines these 3 commands into single request
    redis2_query auth '';
    redis2_query select 0;
    redis2_query set $key $echo_request_body;
    redis2_query expire $key $srcache_expire;

    # Pass the request to redis
    redis2_pass 172.17.0.6:6379;

}

Then I open redis-cli to monitor incoming requests:

$ redis-cli
> monitor
1478704802.63890 [0 172.17.0.8:54906] "select" "0"
1478704802.638100 [0 172.17.0.8:54906] "get" "wp_:nginx:httpsGETwordpress.test/robots.txt"
1478704802.638122 [0 172.17.0.8:54900] "auth" ""
1478704802.638137 [0 172.17.0.8:54900] "select" "0"
1478704802.638148 [0 172.17.0.8:54900] "set" "wp_:nginx:httpsGETwordpress.test/robots.txt" "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nExpires: Thu, 19 Nov 1981 08:52:00 GMT\r\nCache-Control: no-store, no-cache, must-revalidate\r\nPragma: no-cache\r\nX-Robots-Tag: noindex, follow\r\nLink: <https://wordpress.test/wp-json/>; rel=\"https://api.w.org/\"\r\nLink: <https://wordpress.test/wp-json>; rel=\"https://github.com/WP-API/WP-API\"\r\nX-Cache: MISS\r\n\r\nUser-agent: *\nDisallow: /wp-admin/\nAllow: /wp-admin/admin-ajax.php\n"
1478704802.638189 [0 172.17.0.8:54900] "expire" "wp_:nginx:httpsGETwordpress.test/robots.txt" ""

And where I should see 10s because of the srcache_max_expire directive or 0 because of the Cache-Control: no-store, no-cache, must-revalidate header instead I see "" as sent to redis.

Version:

nginx -V
nginx version: openresty/1.11.2.1
built by gcc 4.9.2 (Debian 4.9.2-10)
built with OpenSSL 1.0.2j  26 Sep 2016
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.60 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.6 --add-module=../ngx_lua_upstream-0.06 --add-module=../headers-more-nginx-module-0.31 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.17 --add-module=../redis2-nginx-module-0.13 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/etc/nginx/luajit/lib --with-http_addition_module --with-http_auth_request_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_geoip_module=dynamic --with-file-aio --with-ipv6 --with-pcre-jit --with-stream --with-stream_ssl_module --with-threads --without-http_autoindex_module --without-http_browser_module --without-http_userid_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_split_clients_module --without-http_uwsgi_module --without-http_scgi_module --without-http_referer_module --user=nginx --group=nginx --sbin-path=/usr/sbin --modules-path=/usr/lib/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx/nginx.lock --http-fastcgi-temp-path=/tmp/nginx/fastcgi --http-proxy-temp-path=/tmp/nginx/proxy --http-client-body-temp-path=/tmp/nginx/client_body --add-module=/tmp/ngx_http_redis-0.3.7-master --add-module=/tmp/ngx_pagespeed-1.11.33.4-beta --with-openssl=/tmp/openssl-1.0.2j
onnimonni commented 7 years ago

This also happens to other requests which don't have any Expires or Cache-Control headers.

onnimonni commented 7 years ago

This is how I fixed it for now.

# HACK: Add default value for caching if $srcache_expire is not set
set_if_empty $srcache_expire '900';
agentzh commented 7 years ago

@onnimonni Will you contribute a patch for this?

onnimonni commented 7 years ago

I will try to. Do you have links or recommendation for the development workflow? Nginx and c++ development isn't my strongest skill but with time I'm sure I can handle this and would be happy to contribute into this module.

agentzh commented 7 years ago

@onnimonni You can check out the .travis.yml file in the repo for an example of setting up the development environment for htis module. Also, the following book chapter can also be helpful when you use the test toolchain:

https://openresty.gitbooks.io/programming-openresty/content/testing/

Thanks!