Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 301 lines (226 sloc) 9.325 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
NAME

    ngx_redis2 - Nginx upstream module for the Redis 2.0 protocol

    *This module is not distributed with the Nginx source.* See the
        installation instructions.

STATUS
    This module is still under development and is considered experimental.

SYNOPSIS

    location /foo {
        set $value 'first';
        redis2_query set one $value;
        redis2_pass 127.0.0.1:6379;
    }

    # GET /get?key=some_key
    location /get {
        set_unescape_uri $key $arg_key; # this requires ngx_set_misc
        redis2_query get $key;
        redis2_pass foo.com:6379;
    }

    # GET /set?key=one&val=first%20value
    location /set {
        set_unescape_uri $key $arg_key; # this requires ngx_set_misc
        set_unescape_uri $val $arg_val; # this requires ngx_set_misc
        redis2_query set $key $val;
        redis2_pass foo.com:6379;
    }

    location /bar {
        # $ is not special here...
        redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
        redis2_pass 127.0.0.1:6379;
    }

    location /bar {
        # variables can be used below and $ is special
        redis2_raw_query 'get one\r\n';
        redis2_pass 127.0.0.1:6379;
    }

    # GET /baz?get%20foo%0d%0a
    location /baz {
        set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
        redis2_raw_query $query;
        redis2_pass 127.0.0.1:6379;
    }

Directives
    redis2_query
        Multiple instances of this directive are allowd in
        a single location. These queries will be pipelined.
        But for now, only the first command's response will
        be returned.

    redis2_raw_query

    redis2_literal_raw_query

    redis2_pass

    redis2_connect_timeout

    redis2_send_timeout

    redis2_read_timeout

    redis2_buffer_size

    redis2_next_upstream

Connection Pool
    You can use Maxim Dounin's excellent ngx_http_upstream_keepalive module
    ( http://mdounin.ru/hg/ngx_http_upstream_keepalive/ )
    with this module to privide TCP connection pool
    for Redis.

    A sample config snippet looks like this

      http {
        upstream backend {
          server 127.0.0.1:6379;

          # a pool with at most 1024 connections
          # and do not distinguish the servers:
          keepalive 1024 single;
        }

        server {
            ...
            location /redis {
                set_unescape_uri $query $arg_query;
                redis2_query $query;
                redis2_pass backend;
            }
        }
      }

Lua Interoperability
    This module can be served as a non-blocking redis2 client
    for ngx_lua ( http://github.com/chaoslawful/lua-nginx-module ).

    Here's an example using a GET subrequest:

        location /redis {
            internal;

            # set_unescape_uri is provided by ngx_set_misc
            set_unescape_uri $query $arg_query;

            redis2_raw_query $query;
            redis2_pass 127.0.0.1:6379;
        }

        location /main {
            content_by_lua '
                local res = ngx.location.capture("/redis",
                    { args = { query = "ping\\r\\n" } }
                )
                ngx.print("[" .. res.body .. "]")
            ';
        }

    Then accessing /main yields

        [+PONG\r\n]

    where "\r\n" is CRLF. That is, this module returns the *raw* TCP responses
    from the remote redis server. For lua-based application developers, they
    may want to utilize the lua-redis-parser module (written in pure C) to
    parse such raw responses into Lua data structures:

        https://github.com/agentzh/lua-redis-parser

    You can also use POST/PUT subrequests to transfer the raw redis
    request via request body, which does not require URI escaping
    and unescaping, thus saving some CPU cycles. Here's such an example:

        location /redis {
            internal;

            # $echo_request_body is provided by the ngx_echo module
            redis2_raw_query $echo_request_body;

            redis2_pass 127.0.0.1:6379;
        }

        location /main {
            content_by_lua '
                local res = ngx.location.capture("/redis",
                    { method = ngx.HTTP_PUT,
                      body = "ping\\r\\n" }
                )
                ngx.print("[" .. res.body .. "]")
            ';
        }

    This yeilds exactly the same output as the previous (GET) sample.

    One can also use Lua to pick up a concrete Redis backend
    based on some complicated hashing rules. For
    instance,

        upstream redis-a {
            server foo.bar.com:6379;
        }

        upstream redis-b {
            server bar.baz.com:6379;
        }

        upstream redis-c {
            server blah.blah.org:6379;
        }

        server {
            ...

            location /redis {
                set_unescape_uri $query $arg_query;
                redis2_query $query;
                redis2_pass $arg_backend;
            }

            location /foo {
                content_by_lua "
                    -- pick up a server randomly
                    local servers = {'redis-a', 'redis-b', 'redis-c'}
                    local i = ngx.time() % #servers + 1;
                    local srv = servers[i]

                    local res = ngx.location.capture('/redis',
                        { args = {
                            query = '...',
                            backend = srv
                          } }
                    )
                    ngx.say(res.body)
                ";
            }
        }

Installation
    1. Grab the nginx source code from nginx.net (<http://nginx.net/>), for
        example, the version 0.8.53 (see nginx compatibility),

    2. and then build the source with this module:

        $ wget 'http://sysoev.ru/nginx/nginx-0.8.53.tar.gz'
        $ tar -xzvf nginx-0.8.53.tar.gz
        $ cd nginx-0.8.53/

        # Here we assume you would install you nginx under /opt/nginx/.
        $ ./configure --prefix=/opt/nginx \
            --add-module=/path/to/redis2-nginx-module

        $ make -j2
        $ make install

    Download the latest version of the release tarball of this module from
    redis2-nginx-module file list
    (<http://github.com/agentzh/redis2-nginx-module/downloads>).

Compatibility
    Redis 2.0, 2.2, and above should work with this module
    without any issues. So is the "Alchemy Database"
    (aka redisql).

    The following versions of Nginx should work with this module:

    * 0.8.x >= 0.8.53

    Earlier versions of Nginx will *not* work.

    If you find that any particular version of Nginx above 0.8.53 does not
    work with this module, please consider reporting a bug.

Report Bugs
    Although a lot of effort has been put into testing and code tuning,
    there must be some serious bugs lurking somewhere in this module. So
    whenever you are bitten by any quirks, please don't hesitate to

    1. send a bug report or even patches to <agentzh@gmail.com>,

    2. or create a ticket on the issue tracking interface
        (<http://github.com/agentzh/redis2-nginx-module/issues>)
        provided by GitHub.

Source Repository
    Available on github at agentzh/redis2-nginx-module
    (<http://github.com/agentzh/redis2-nginx-module>).

TODO
    * Add the "redis2_as_json" directive to allow emitting JSON directly.
    * Add full support for redis pipelining in the response parser.

AUTHORS
    Yichun "agentzh" Zhang (章亦春) < agentzh@gmail.com >

Getting involved
    You'll be very welcomed to submit patches to the author or just ask for
    a commit bit to the source repository on GitHub.

Copyright & License
    Copyright (c) 2010, Taobao Inc., Alibaba Group ( http://www.taobao.com
    ).

    Copyright (c) 2010, Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>.

    This module is licensed under the terms of the BSD license.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

    * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SEE ALSO
    * a redis response parser for Lua: https://github.com/agentzh/lua-redis-parser
Something went wrong with that request. Please try again.