FRiCKLE / ngx_postgres

upstream module that allows nginx to communicate directly with PostgreSQL database.
http://labs.frickle.com/nginx_ngx_postgres/
BSD 2-Clause "Simplified" License
545 stars 122 forks source link

Unable to reproduce sample configuration #3 #40

Closed wasade closed 9 years ago

wasade commented 9 years ago

I'm unable to get something like sample configuration #3 to work. I apologize in advance if this question is a bit naive -- I'm not extremely familiar with nginx. When I include eval_subrequest_in_memory off; or the subsequent directives, I receive:

nginx: [emerg] unknown directive "eval_subrequest_in_memory" in /foo/nginx.conf:70

I'm using openresty, and from what I'm reading, it sounds like nginx-eval-module (agentzh's fork) is part of openresty. Below are the relevant version details and modules:

$ nginx -V
nginx version: openresty/1.7.10.2
built by clang 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -I/usr/local/include' --add-module=../ngx_devel_kit-0.2.19 --add-module=../echo-nginx-module-0.58 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.29 --add-module=../form-input-nginx-module-0.11 --add-module=../encrypted-session-nginx-module-0.04 --add-module=../ngx_postgres-1.0rc6 --add-module=../srcache-nginx-module-0.30 --add-module=../ngx_lua-0.9.16 --add-module=../ngx_lua_upstream-0.03 --add-module=../headers-more-nginx-module-0.26 --add-module=../array-var-nginx-module-0.04 --add-module=../memc-nginx-module-0.16 --add-module=../redis2-nginx-module-0.12 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.06 --with-ld-opt=-L/usr/local/lib --with-http_ssl_module

Bigger picture, what I'd like to do is source a URL from a table and redirect for a download. I'm less concerned about how this is accomplished, and more concerned about being able to do it. As a practical example, say I have the following table:

create table foo (
    id varchar,
    url varchar
)

What I'd like to be able to do from nginx is something along these lines:

location ~ /foo/(?<id>\d+) {
    postgres_output none;
    postgres_pass    database;
    postgres_escape $escaped $id;
    postgres_query  GET "SELECT url FROM foo WHERE id=$escaped";
    postgres_rewrite no_rows 410;
    postgres_set $url 0 0 required;
    return 301 $url;   
}

...however, I realize that (for reasons I don't fully grok) that return and the postgres_* statements aren't compatible due to how nginx processes things.

Any help would be greatly appreciated, thanks!

agentzh commented 9 years ago

@wasade That directive is provided by the ngx_eval module:

https://github.com/openresty/nginx-eval-module

Try including this module in your nginx or openresty build.

wasade commented 9 years ago

Right, its supposed to be included in openresty but it doesn't appear to work, or am I missing something obvious? On Jul 28, 2015 12:03 AM, "Yichun Zhang" notifications@github.com wrote:

Closed #40 https://github.com/FRiCKLE/ngx_postgres/issues/40.

— Reply to this email directly or view it on GitHub https://github.com/FRiCKLE/ngx_postgres/issues/40#event-366854098.

agentzh commented 9 years ago

@wasade No. The ngx_eval module is not a default OpenResty component on purpose. Please see

https://openresty.org/#Components

It's not included because use of ngx_lua is recommended instead of ngx_eval. The ngx_eval module is the old day solution. See the corresponding documentation of my fork of ngx_eval:

https://github.com/openresty/nginx-eval-module#use-lua-instead

wasade commented 9 years ago

Thanks, @agentzh. I confess, I'm not seeing any text on that openresty link regarding "ngx_eval" or even "eval" which perhaps is driving some of the confusion. Thanks for the link on using "ngx_lua" -- that looks like it'll do what I need. Sorry for the confusion and thank you for the rapid responses!

agentzh commented 9 years ago

@wasade Yes, the examples in ngx_postgrres's README deserves treatment to avoid such confusions.