#user nobody; worker_processes 1; syslog local0 nginx; error_log syslog:error|logs/error.log; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; access_log syslog:info main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream my_pgsql_upstream { postgres_server 127.0.0.1 dbname=test } server { listen 8080; server_name localhost; access_log syslog:info main; location / { root html; index index.html index.htm; } location ~ '^/test/(\d+)$' { set $id $1; set_form_input $test_name_p name; set_quote_pgsql_str $test_name $test_name_p; postgres_pass my_pgsql_upstream; postgres_query GET "select * from test where id = $id"; postgres_query POST "update test set name = $test_name where id = $id"; rds_json on; } encrypted_session_expires 900; # in sec # default: 1d (1 day) location = /auth_test { internal; set_form_input $test_name_p name; postgres_escape $auth_name $test_name_p; postgres_query "select coalesce( ( select id from test where name = $auth_name ), 0);" ; postgres_pass my_pgsql_upstream; postgres_output value; } location /auth { lua_need_request_body on; set $uid 0; access_by_lua ' local res = ngx.location.capture("/auth_test", {method = ngx.HTTP_POST}) ngx.var.uid = res.body if res.body == "0" then ngx.status = 503; ngx.say("FAIL") ngx.exit(200) end '; set_encrypt_session $session $uid; set_encode_base32 $session; echo " { \"login\" : $session } "; } location /auth_only { set_decode_base32 $session $cookie_login; # from the ngx_set_misc module set_decrypt_session $raw $session; if ($raw != '') { return 200; } } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }