Skip to content

Instantly share code, notes, and snippets.

@isublimity
Created August 6, 2020 11:58
Show Gist options
  • Save isublimity/3d56066c07e65d5701833da8a309123d to your computer and use it in GitHub Desktop.
Save isublimity/3d56066c07e65d5701833da8a309123d to your computer and use it in GitHub Desktop.
Nginx find in json deviceID for sharding
if ngx.req.get_method() ~= "POST" then
return
end
-- read request body
ngx.req.read_body()
local data = ngx.req.get_body_data()
-- regexp
if data then
local f = string.match(data,ngx.var.MATH_REGEXP)
if f then
-- Set Var to Nginx
ngx.var.UUID = f;
end
end
-- Dump
ngx.say("Hello <b>world</b>! UUID = " .. ngx.var.UUID);
#user www-data;
worker_processes 1;
worker_priority 1;
worker_rlimit_nofile 1000;
timer_resolution 100ms;
daemon off;
master_process on;
error_log /tmp/nginx-error.log;
#pid /var/run/nginx.pid;
events {
worker_connections 256;
}
http {
access_log /tmp/nginx-access.main.log;
error_log /tmp/nginx-error.main.log;
server {
set $UUID false;
location /hello {
set $MATH_REGEXP "\"deviceID\":\"([^\"]+)\"";
client_body_buffer_size 3M;
client_max_body_size 3M;
content_by_lua_file /tmp/nginx/jparseuuid.lua;
if ($UUID != false )
{
# use `upstream->hash $UUID`;
}
}
}
}
curl http://localhost:8000/hello --data '{"username":"xyz","deviceID":"qew-ew-wee2qwe1","2323":"23"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment