Skip to content

osokin/ngx_http_redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nginx HTTP redis module
--

Description:
--

The nginx HTTP redis module for caching with redis,
https://redis.io/.

The redis protocol, https://redis.io/topics/protocol,
not yet fully implemented, but AUTH, GET and SELECT commands only.



Installation:
--

You'll need to re-compile Nginx from source to include this module.
Modify your compile of Nginx by adding the following directive
(modified to suit your path of course):

./configure --add-module=/absolute/path/to/ngx_http_redis
make
make install



Usage:
--

Example 1.


http
{
 ...
        server {
                location / {
                        set $redis_key  "$uri?$args";
                        redis_pass      127.0.0.1:6379;
                        error_page      404 502 504 = @fallback;
                }

                location @fallback {
                        proxy_pass      backed;
                }
        }
}


Example 2.

Capture User-Agent from an HTTP header, query to redis database
for lookup appropriate backend.

Eval module (http://www.grid.net.ru/nginx/eval.en.html) required.

http
{
 ...
    upstream redis {
        server  127.0.0.1:6379;
    }

    server {
     ...
        location / {

	    eval_escalate on;

	    eval $answer {
		set $redis_key	"$http_user_agent";
		redis_pass	redis;
	    }

            proxy_pass $answer;
        }
        ...
     }
}


Example 3.

Compile nginx with ngx_http_redis and ngx_http_gunzip_filter modules
(http://mdounin.ru/hg/ngx_http_gunzip_filter_module/).
Gzip content and put it into redis database.

% cat index.html
<html><body>Hello from redis!</body></html>
% gzip index.html
% cat index.html.gz | redis-cli -x set /index.html
OK
% cat index.html.gz | redis-cli -x set /
OK
%

Using following configuration nginx get data from redis database and
gunzip it.

http
{
 ...
    upstream redis {
        server  127.0.0.1:6379;
    }

    server {
     ...
        location / {
                gunzip on;
                redis_gzip_flag 1;
                set $redis_key "$uri";
                redis_pass redis;
    }
}


Example 4.

The same as Example 1 but with AUTH.

http
{
 ...
        server {
                location / {
                        set $redis_auth somepasswd;
                        set $redis_key  "$uri?$args";
                        redis_pass      127.0.0.1:6379;
                        error_page      404 502 504 = @fallback;
                }
}


Development and testing
--

1. Clone nginx-tests

   git clone https://github.com/nginx/nginx-tests.git


2. Build [and install] nginx

   Installation of nginx is the optional step.

   For FreeBSD ports tree:

   cd /usr/ports/www/nginx-devel && make [ && make install ]



3. Run the following command

   PERL5LIB=/path/to/nginx-tests/dir \
   TEST_NGINX_VERBOSE=1 \
   TEST_NGINX_LEAVE=1 \
   TEST_NGINX_BINARY=/path/to/nginx/binary \
   TEST_NGINX_MODULES=/path/to/nginx/modules/directory \
   TEST_NGINX_GLOBALS="load_module /path/to/nginx/modules/directory/ngx_http_redis_module.so;" \
   prove

   Here's the example, when nginx built from the FreeBSD ports tree

   PERL5LIB=/home/nginx/github/nginx-tests/lib:${PERL5LIB} \
   TEST_NGINX_VERBOSE=1 \
   TEST_NGINX_LEAVE=1 \
   TEST_NGINX_BINARY=/usr/local/sbin/nginx \
   TEST_NGINX_MODULES=/usr/local/libexec/nginx \
   TEST_NGINX_GLOBALS="load_module /usr/local/libexec/nginx/ngx_http_redis_module.so;" \
   prove -prvv



Thanks to:
--

Maxim Dounin
Vsevolod Stakhov
Ezra Zygmuntowicz



Special thanks to:
--
Evan Miller for his "Guide To Nginx Module Development" and "Advanced Topics
In Nginx Module Development"
Valery Kholodkov for his "Nginx modules development"

About

The ngx_http_redis module provides support for caching with redis

Resources

License

Stars

Watchers

Forks

Packages

No packages published