-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.conf
106 lines (84 loc) · 3.11 KB
/
nginx.conf
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
106
events {
worker_connections 1024;
}
http {
lua_shared_dict room_cache 10m;
resolver 127.0.0.11 ipv6=off;
limit_req_zone $binary_remote_addr zone=ws_rate_limit:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=api_rate_limit:10m rate=5r/s;
upstream app_1 {
server app_1:8080;
}
upstream app_2 {
server app_2:8081;
}
server {
listen 80;
root /dev/null;
error_page 400 404 500 503 504 /error.html;
location = /error.html {
root /usr/local/openresty/nginx/html;
internal;
ssi on;
}
location /create-room {
limit_req zone=api_rate_limit burst=10 nodelay;
proxy_pass http://app_1;
}
location /join-room {
limit_req zone=api_rate_limit burst=10 nodelay;
proxy_pass http://app_1;
}
location /health1 {
proxy_pass http://app_1;
}
location /health2 {
proxy_pass http://app_2;
}
location /ws {
limit_req zone=ws_rate_limit burst=20 nodelay;
access_log /usr/local/openresty/nginx/logs/access.log combined buffer=512k flush=1s;
error_log /usr/local/openresty/nginx/logs/error.log notice;
set $backend "";
set $room_id $arg_roomId;
access_by_lua_block {
local room_cache = ngx.shared.room_cache
local redis = require "resty.redis"
local room_id = ngx.var.room_id
local instance = room_cache:get('room:' .. room_id)
if not instance then
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("redis", 6379)
if not ok then
ngx.log(ngx.ERR, "Redis connection failed: ", err)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
instance, err = red:get('room:' .. room_id .. ':affinity')
if not instance then
ngx.log(ngx.ERR, "Redis GET failed: ", err)
ngx.exit(ngx.HTTP_BAD_REQUEST)
end
if instance == "" then
ngx.log(ngx.ERR, "Empty room affinity")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
room_cache:set('room:' .. room_id, instance, 60)
red:close()
end
if instance == "instance_1" then
ngx.var.backend = "app_1"
elseif instance == "instance_2" then
ngx.var.backend = "app_2"
else
ngx.log(ngx.ERR, "Invalid instance: ", instance)
ngx.exit(ngx.HTTP_NOT_FOUND)
end
}
proxy_pass http://$backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}