Skip to content

Instantly share code, notes, and snippets.

@moonbingbing
Created November 2, 2012 03:04

Revisions

  1. @moonming moonming created this gist Nov 2, 2012.
    27 changes: 27 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    worker_processes 1;


    events {
    worker_connections 64;
    }

    http {
    send_timeout 1s;
    server_tokens off;
    client_body_buffer_size 128k;

    upstream database {
    postgres_server localhost:5432 dbname=test
    user=adminpassword=test123;
    postgres_keepalive max=80 mode=single overflow=ignore;
    }

    lua_package_path "$prefix/?.luac;$prefix/?.lua;;";

    include mime.types;

    log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $request_time $request_body';

    include ngx_php_win.conf;
    include ngx_lua_win.conf;
    }
    23 changes: 23 additions & 0 deletions ngx_lua_win.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    server {
    listen 80;
    access_log off;#/var/log/nginx/access.log access;
    gzip on;
    gzip_types text/plain application/x-javascript text/css application/xml;

    location /postgres {
    internal;
    default_type text/html;
    set_by_lua $query_sql 'return ngx.unescape_uri(ngx.var.arg_sql)';
    postgres_pass database;
    rds_json on;
    postgres_query $query_sql;
    }

    location /hello {
    access_log off;
    default_type text/html;
    content_by_lua '
    ngx.say("<p>hello, world</p>")
    ';
    }
    }
    22 changes: 22 additions & 0 deletions query_db.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    function query_from_db(sql)
    local res = ngx.location.capture('/postgres',
    { args = {sql = sql } }
    )

    local status = res.status
    local body = json.decode(res.body)

    if status == 200 then
    status = true
    else
    status = false
    end
    return status, body
    end

    function client_heartbeat(mid)
    common.query_from_db("BEGIN")
    local sql = [[UPDATE clients SET update_time = now() WHERE midn = ']] .. mid .. [[']]
    common.query_from_db(sql)
    common.query_from_db("COMMIT")
    end