Skip to content

Instantly share code, notes, and snippets.

@narate
Created November 30, 2015 14:55
Show Gist options
  • Save narate/24529e1b78e071b72171 to your computer and use it in GitHub Desktop.
Save narate/24529e1b78e071b72171 to your computer and use it in GitHub Desktop.
Simple nginx config with lua script
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
proxy_cache_path /data/cache/page levels=1:2 keys_zone=python:10m max_size=1g inactive=5m;
server {
access_log logs/access.log;
listen 8080;
lua_code_cache off;
location / {
echo "Hello, World";
}
location ~ ^/api/v1/(.*)$ {
# path to lua script
content_by_lua_file lua/$1.lua;
}
location /page/ {
set $cache_key $scheme$host$uri$is_args$args;
proxy_cache_key $cache_key;
proxy_cache_valid 30m;
proxy_cache python;
proxy_pass http://127.0.0.1:8004/;
}
}
}
-- lua/post.lua
local cjson = require 'cjson'
ngx.req.read_body()
local data = ngx.req.get_body_data()
if not data then
ngx.say(cjson.encode({ error = 1, message = 'POST data not found' }))
ngx.exit(ngx.HTTP_OK)
end
ngx.say("BODY DATA : " ..data)
local json = cjson.decode(data)
if not json.username then
ngx.say(cjson.encode({ error = 1, message = 'Username not found' }))
end
ngx.say('Do signup.')
ngx.say('User info.')
ngx.say(cjson.encode(json))
-- lua/render.lua
local template = require "resty.template"
local data = {
title = 'F5, Demo',
message = "Hello, World!"
}
template.render([[
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{message}}</h1>
</body>
</html>
]], data)
@narate
Copy link
Author

narate commented Nov 30, 2015

How to run

$ mkdir -p web
$ cd web
$ mkdir -p conf logs lua
$ ls lua
post.lua render.lua
$ ls conf
nginx.conf
$ nginx -c conf/nginx.conf -p $PWD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment