Giter Site home page Giter Site logo

nginx-eval-module's Introduction

Status
    This module is experimental and production use is discouraged.
    If you want similar (but more powerful) functionalities, see
    the ngx_lua module instead:

        http://github.com/chaoslawful/lua-nginx-module

Synopsis

    # an example for working with the ngx_drizzle + ngx_rds_json
    # modules, but you must put ngx_rds_json *after*
    # ngx_eval during nginx configure, for example:
    #     ./configure --add-module=/path/to/nginx-eval-module \
    #           --add-module=/path/to/rds-json-nginx-module \
    #           --add-module=/path/to/drizzle-nginx-module
    location = /mysql {
        eval_subrequest_in_memory off;
        eval_override_content_type text/plain;
        eval_buffer_size 4k; # default 4k, truncated if overflown
        eval $res {
            drizzle_query "select * from cats";
            drizzle_pass my_mysql_backend;
            rds_json_on;
        }
        # now $res holds the JSON formatted result set
        if ($res ~ '"Tom"') {
            echo "Found the Tom cat!";
            break;
        }
        echo "The Tom cat is missing!";
    }

   # an example for working with the ngx_postgres module
   location = /login {
       eval_subrequest_in_memory off;
       eval_override_content_type text/plain;
       eval_buffer_size 1k;
       eval $uid {
           postgres_query "select id
               from users
               where name=$arg_name and pass=$arg_pass";
           postgres_pass pg_backend;
           postgres_output value 0 0;
       }
       if ($uid !~ '^\d+$') {
           rewrite ^ /relogin redirect; break;
       }
       # your content handler settings...
    }

Description

    This fork of ngx_eval can work with any content handlers and
    even with filters enabled as long as you put ngx_eval *before*
    your filter modules during nginx configure, for instance

    ./configure --prefix=/opt/nginx \
            --add-module=/path/to/this/nginx-eval-module \
            --add-module=/path/to/your/filter/module \
            --add-module=/path/to/your/other/filters

such that ngx_eval's filter works *after* your filter modules.

Limitations
    *   The contents of subrequests issued from within the eval block
        (like those fired by echo_subrequest) won't be
        captured properly.

Compatibility
    The following versions of Nginx should work with this module:

    *   0.9.x (last tested: 0.9.4)
    *   0.8.0 ~ 0.8.41, 0.8.54+ (0.8.42 ~ 0.8.53 requires patching, see below)
                        (last tested: 0.8.54)

    *   0.7.x >= 0.7.21 (last tested: 0.7.68)

Note that nginx 0.8.42 ~ 0.8.53 won't work due to a famous regression appeared
since 0.8.42: <http://forum.nginx.org/read.php?29,103078,103078 >,
but fortunately a patch is available for nginx 0.8.53:

    http://agentzh.org/misc/nginx/nginx-0.8.53-rewrite_phase_fix.patch

This one-line patch should also be able to apply cleanly to other versions
of nginx 0.8.42 ~ 0.8.53.

Nowadays we prefer ngx_lua to do the tasks originally done by ngx_eval
because of various inherent limitations in ngx_eval (and yeah, it also
requires patching the core for nginx 0.8.42+, sigh).

Here's a small example using ngx_lua:

 location = /filter-spam {
    internal;
    proxy_pass http://blah.blah/checker;
 }

 location / {
   rewrite_by_lua '
       local res = ngx.location.capture("/filter-spam")
       if res.status ~= ngx.HTTP_OK or res.body == nil then
         return
       end
       if string.match(res.body, "SPAM") then
         return ngx.redirect("/terms-of-use")
       end
   ';

   fastcgi_pass ...;
 }

this is roughly equivalent to

 location / {
    eval $res {
        proxy_pass http://blah.blah/checker;
    }
    if ($res ~ 'SPAM') {
        rewrite ^ /terms-of-use redirect;
        break;
    }

    fastcgi_pass ...;
 }

Even though the Lua example is a little longer but is much more flexible
and stable.

Original ngx_eval documentation:

Documentation for this module could be found under following URLs:

  * English:

    http://www.grid.net.ru/nginx/eval.en.html

  * Russian:

    http://www.grid.net.ru/nginx/eval.ru.html

This work is commissioned by gadu-gadu.pl

nginx-eval-module's People

Contributors

agentzh avatar piotrsikora avatar vkholodkov avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.