r->headers_out.content_type.len = sizeof("image/gif") - 1;
r->headers_out.content_type.data = (u_char *) "image/gif";
Is there a special way to malloc the .data so that it
will be freed automatically when the request is complete?
If so, then I can simply set my headers and pass along to the next
phase?
No. X-Accel-Redirect is special header understood by upstream
module (proxy, fastcgi) in backend responses. In your own module
you have to use ngx_http_internal_redirect() function for this.
Any simple example modules I can use as a starting point?
For simple content generation see ngx_http_empty_gif_module.c,
ngx_http_static_module.c, ngx_http_flv_module.c.
For simple examples of internal redirect usage see
ngx_http_index_module.c, ngx_http_random_index_module.c.
Hi,
Maxim already answered most of your questions regarding writing a module
like this from scratch. I would like to add that you can already do
this,
without writing new module, by misusing Maxim’s excellent
ngx_http_auth_request_module [1] and ngx_postgres [2].
There are two “but”:
Required “postgres_set” directive is still available only in my local
repository. I intend to push it to GitHub today or tomorrow, after
adding
some test cases, so stay tuned
You need to use “proxy_pass” for now. Ideally, you should create your
own
mini-module that would serve static files based on complex_value
($location). This should be mostly copy & paste of
“ngx_http_static_handler”
from nginx’s ngx_http_static_module.c.
On Wed, Jun 16, 2010 at 05:13:40PM +0200, Piotr S. wrote:
Hi,
Maxim already answered most of your questions regarding writing a
module like this from scratch. I would like to add that you can
already do this, without writing new module, by misusing Maxim’s
excellent ngx_http_auth_request_module [1] and ngx_postgres [2].
Required “postgres_set” directive is still available only in my
local repository. I intend to push it to GitHub today or tomorrow,
after adding some test cases, so stay tuned
I believe this may be handled by eval as long as postgres module
is able to return something it understands.
You need to use “proxy_pass” for now. Ideally, you should create
your own mini-module that would serve static files based on
complex_value ($location). This should be mostly copy & paste of
“ngx_http_static_handler” from nginx’s ngx_http_static_module.c.
Something like alias /path/to/$location; should do the trick.
Also, with eval it’s possible to use regular rewrites instead.
I believe Valery K.'s eval module
(http://grid.net.ru/nginx/eval.en.html) would fit better here than
auth request module.
(..)
I believe this may be handled by eval as long as postgres module
is able to return something it understands.
I can’t agree with you here. ngx_postgres can either return single value
in
clear text or whole result-set in RDS format (which can be converted to
JSON
using ngx_rds_json [1]). Since we need more than one value, we would
need to
either do multiple queries for each value or use JSON. Of course, we
could
extract values from JSON using ngx_lua [2], but this seems like quite an
overhead for something that could be easily achieved by misusing your
module.
Something like alias /path/to/$location; should do the trick.
I tried that before suggesting “proxy_pass”.
Unfortunately both “alias $location;” and “alias /$location;” results in
“/etc/passwdindex.html” is not a directory while sending to client".
On Wed, Jun 16, 2010 at 05:46:32PM +0200, Piotr S. wrote:
value in clear text or whole result-set in RDS format (which can be
converted to JSON using ngx_rds_json [1]). Since we need more than
one value, we would need to either do multiple queries for each
value or use JSON. Of course, we could extract values from JSON
using ngx_lua [2], but this seems like quite an overhead for
something that could be easily achieved by misusing your module.
I wrote “as long as …” on purpose, as I have no idea what
postrgess module is able to produce, sorry.
But as long as it’s able to return single value in clear text it
should be possible to write select which will return all needed
data separated by newlines. This should be understood by eval
module easily.
Something like alias /path/to/$location; should do the trick.
I tried that before suggesting “proxy_pass”.
Unfortunately both “alias $location;” and “alias /$location;”
results in “/etc/passwdindex.html” is not a directory while sending
to client".
This is caused by index module and only applies when uri ends with
‘/’. Workarounds are obvious, but I doubt they are actually
needed in this particular case.
Oops. You’re right. I didn’t notice that eval module allowed multiple
variables until now. In that case, it might be indeed a better fit than
ngx_http_auth_request_module.
…or not. It seems that eval module evaluates multiple variables only
for
“application/x-www-form-urlencoded” content, so it won’t work with clear
text values separated by new lines.
On Wed, Jun 16, 2010 at 06:47:29PM +0200, Piotr S. wrote:
[…]
}
“GET /aaa” results in:
[…]
*4 open() “/etc/passwdaaa” failed (2: No such file or directory)"
No, it’s not bug, it works as documented. Alias specifies full
path only in regexp locations (with captures). For normal locations
it specifies path to point matched by location (even if alias
contains variables).
I wrote “as long as …” on purpose, as I have no idea what
postrgess module is able to produce, sorry.
But as long as it’s able to return single value in clear text it
should be possible to write select which will return all needed
data separated by newlines. This should be understood by eval
module easily.
Oops. You’re right. I didn’t notice that eval module allowed multiple
variables until now. In that case, it might be indeed a better fit than
ngx_http_auth_request_module.
One note though, you will need to use agentzh’s fork of the eval module
[1],
because ngx_postgres currently doesn’t work with subrequests in memory.
This is caused by index module and only applies when uri ends with
‘/’. Workarounds are obvious, but I doubt they are actually
needed in this particular case.
Yes, my previous error message was produced by index module,
but the issue exists and should be probably considered a bug:
location / {
set $file “/etc/passwd”;
alias $file;
}
No, it’s not bug, it works as documented. Alias specifies full
path only in regexp locations (with captures). For normal locations
it specifies path to point matched by location (even if alias
contains variables).
Uhm… I stand corrected. Russian documentation indeed says that.