quick start on writing a module?

Piotr Sikora piotr.sikora at frickle.com
Wed Jun 16 19:13:40 MSD 2010


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":
1) 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 :)
2) 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.

[1] http://mdounin.ru/hg/ngx_http_auth_request_module/
[2] http://labs.frickle.com/nginx_ngx_postgres/

Best regards,
Piotr Sikora < piotr.sikora at frickle.com >


--- configuration ---
upstream pgsql {
  postgres_server  127.0.0.1 dbname=test user=test password=test;
}

server {
  listen  80;

  location = /auth {
    internal;
    postgres_pass   pgsql;
    postgres_query  "SELECT 'c-d', '123456', '0.1', '/etc/passwd'";
    postgres_set    $pg_disposition 0 0 required;
    postgres_set    $pg_etag 0 1 required;
    postgres_set    $pg_version 0 2 required;
    postgres_set    $pg_location 0 3 required;
  }

  location / {
    auth_request      /auth;
    auth_request_set  $disposition $pg_disposition;
    auth_request_set  $etag $pg_etag;
    auth_request_set  $version $pg_version;
    auth_request_set  $location $pg_location;
    add_header        "Content-Disposition" $disposition;
    add_header        "Etag" $etag;
    add_header        "X-mod_asset-version" $version;
    proxy_pass        http://127.0.0.1:81/$location;
  }
}

# proxy for serving static files
server {
  listen    81;

  location / {
    root    /;
  }
}


--- output ---
$ curl -v http://localhost/
* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.5
> Host: localhost
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/0.8.41
< Date: Wed, 16 Jun 2010 16:55:30 GMT
< Content-Type: text/plain
< Connection: keep-alive
< Content-Length: 3078
< Last-Modified: Sat, 15 May 2010 01:27:40 GMT
< Accept-Ranges: bytes
< Content-Disposition: c-d
< Etag: 123456
< X-mod_asset-version: 0.1
<
root:*:0:0:Charlie &:/root:/bin/ksh
(..)
 




More information about the nginx mailing list