-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnginx.conf
More file actions
106 lines (97 loc) · 3.42 KB
/
nginx.conf
File metadata and controls
106 lines (97 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## Minimal configuration of nginx (with lua module) to have a proxy
## that can suspend/resume the traffic to your backend API or app.
##
## The lua code is inlined for simplicity, it can be moved to files
## if you want to integrate the insomnia logic to your own nginx config
## file.
worker_processes 2;
## be aware of the numer of worker connections, it has to be high enough
## to accomodate the maximum concurrency that you will have. When you are
## on suspended traffic, the connections pile up! If you have a 10req/s,
## and are suspended for 30 sec, you need at least 30*10 worker_connections.
events { worker_connections 512; }
http {
## upstream of your backend
## for the sake of the example we run it against a sentiment API
## (https://github.com/solso/sentiment-api-example)
upstream api-backend {
server api-sentiment.3scale.net:80 max_fails=5 fail_timeout=30;
}
## initialize shared state variable that controls whether nginx is
## awake or sleeping for the locations that use insommia_check
lua_shared_dict state 12k;
init_by_lua_block {
ngx.shared.state:set("insomnia", false)
}
server {
listen 8080;
server_name your_server_name;
## ---
## declare the API to interact with insomnia:
##
## To fetch the current state:
## GET /insommia/YOUR_SHARED_SECRET
##
## To put it to sleep (suspend the traffic to your backend)
## PUT -d "sleep" /insommia/YOUR_SHARED_SECRET
##
## To awake it (resume the traffic to your backend)
## PUT -d "awake" /insommia/YOUR_SHARED_SECRET
##
## Do not forget to change YOUR_SHARED_SECRET to something else :)
##
location ~/insomnia/YOUR_SHARED_SECRET {
if ($request_method = GET) {
content_by_lua_block {
if (ngx.shared.state:get("insomnia")==true) then
local message = "sleeping"
ngx.header.content_length = string.len(message)+1
ngx.say(message)
else
local message = "awake"
ngx.header.content_length = string.len(message)+1
ngx.say(message)
end
}
}
if ($request_method = PUT) {
content_by_lua_block {
ngx.req.read_body()
content = ngx.req.get_body_data()
if (content == "sleep") then
ngx.shared.state:set("insomnia", true)
local message = "sleeping"
ngx.header.content_length = string.len(message)+1
ngx.say(message)
else
if (content == "awake") then
ngx.shared.state:set("insomnia", false)
local message = "awake"
ngx.header.content_length = string.len(message)+1
ngx.say(message)
else
local message = "body must be either: awake or sleep"
ngx.header.content_length = string.len(message)+1
ngx.say(message)
end
end
}
}
}
## ---
## Here you have you own location, in this particular case, all traffic is
## proxy passed to api-backend (api-sentiment.3scale.net)
location / {
## ---
## if you want the location to be subjected to insomnia control, you must
## add the lines below. As you can see, it cannot be simpler.
access_by_lua_block {
while(ngx.shared.state:get("insomnia")==true) do
ngx.sleep(0.2)
end
}
## ---
proxy_pass http://api-backend;
}
}
}