-
Notifications
You must be signed in to change notification settings - Fork 8
/
nginx.conf
77 lines (65 loc) · 2.57 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
worker_processes 16;
error_log /usr/local/openresty/nginx/logs/perror.log;
events {
worker_connections 1024;
}
stream {
log_format tcp_proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /usr/local/openresty/nginx/logs/paccess.log tcp_proxy;
open_log_file_cache off;
upstream backend{
server 106.52.172.214:8088;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local host = ""
local port = 0
host = ngx.ctx.proxy_host
port = ngx.ctx.proxy_port
-- 设置 balancer
local ok, err = balancer.set_current_peer(host, port)
-- local ok=0
if not ok then
ngx.log(ngx.ERR, "failed to set the peer: ", err)
end
}
}
server {
preread_by_lua_block{
local redis = require "resty.redis"
local red = redis:new()
red:set_timeouts(1000, 1000, 1000)
local ok, err = red:connect("192.168.112.3", 6379)
if not ok then
ngx.log(ngx.ERR,"failed to connect: ", err)
return red:close()
end
local rkey = "use_proxy"
local res, err = red:hkeys(rkey)
if not res then
ngx.log(ngx.ERR,"res num error : ", err)
return red:close()
end
local radmnum = math.randomseed(tonumber(tostring(ngx.now()):reverse():sub(1, 6)))
local proxy = res[math.random(#res)]
-- ngx.log(ngx.ERR,"res num : ", proxy)
local colon_index = string.find(proxy, ":")
local proxy_ip = string.sub(proxy, 1, colon_index - 1)
local proxy_port = string.sub(proxy, colon_index + 1)
ngx.log(ngx.ERR,"redis data = ", proxy_ip, ":", proxy_port);
ngx.ctx.proxy_host = proxy_ip
ngx.ctx.proxy_port = proxy_port
local ok, err = red:close()
if not ok then
ngx.log(ngx.ERR,"failed to close: ",tostring(err))
return
end
}
listen 0.0.0.0:80;
proxy_connect_timeout 3s;
proxy_timeout 10s;
proxy_pass backend;
}
}