mirror of
https://gitgud.io/fatchan/haproxy-protection.git
synced 2025-05-09 02:05:37 +00:00
Add geo routing to different backends for same domain
This commit is contained in:
@ -47,11 +47,17 @@ services:
|
|||||||
nginx:
|
nginx:
|
||||||
ports:
|
ports:
|
||||||
- 1081:80
|
- 1081:80
|
||||||
- 1082:80
|
|
||||||
image: "nginx:latest"
|
image: "nginx:latest"
|
||||||
volumes:
|
volumes:
|
||||||
- ./nginx:/usr/share/nginx/html
|
- ./nginx:/usr/share/nginx/html
|
||||||
|
|
||||||
|
nginx2:
|
||||||
|
ports:
|
||||||
|
- 1082:80
|
||||||
|
image: "nginx:latest"
|
||||||
|
volumes:
|
||||||
|
- ./nginx2:/usr/share/nginx/html
|
||||||
|
|
||||||
varnish:
|
varnish:
|
||||||
network_mode: host
|
network_mode: host
|
||||||
image: varnish:latest
|
image: varnish:latest
|
||||||
|
@ -165,7 +165,7 @@ backend haproxy-to-varnish-cache
|
|||||||
server varnish unix@/shared-sockets/haproxy-to-varnish-cache.sock check observe layer7 inter 1s
|
server varnish unix@/shared-sockets/haproxy-to-varnish-cache.sock check observe layer7 inter 1s
|
||||||
|
|
||||||
backend servers
|
backend servers
|
||||||
balance leastconn
|
balance roundrobin
|
||||||
use-server %[lua.get_server_names] if TRUE
|
use-server %[lua.get_server_names] if TRUE
|
||||||
|
|
||||||
backend bot_check_post_throttle
|
backend bot_check_post_throttle
|
||||||
|
@ -1 +1,2 @@
|
|||||||
localhost 127.0.0.1:1081
|
localhost 127.0.0.1:1081;OC
|
||||||
|
localhost 127.0.0.1:1082;NA
|
||||||
|
@ -1 +1 @@
|
|||||||
localhost admin
|
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
|
||||||
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>test</title>
|
<title>test</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
|
||||||
<p>hello, world</p>
|
<body>
|
||||||
</body>
|
<p>hello from nginx 1</p>
|
||||||
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
BIN
nginx2/favicon.ico
Normal file
BIN
nginx2/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
14
nginx2/index.html
Normal file
14
nginx2/index.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>test</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<p>hello from nginx 2</p>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -2,18 +2,36 @@ package.path = package.path .. "./?.lua;/etc/haproxy/scripts/?.lua;/etc/haproxy
|
|||||||
|
|
||||||
local bot_check = require("bot-check")
|
local bot_check = require("bot-check")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
local server_cn_split_regex = "([^;]+);(%u%u)$"
|
||||||
local backends_map = Map.new('/etc/haproxy/map/backends.map', Map._str)
|
local backends_map = Map.new('/etc/haproxy/map/backends.map', Map._str)
|
||||||
|
|
||||||
function get_server_names(txn)
|
function get_server_names(txn)
|
||||||
local key = txn.sf:hdr("Host")
|
local key = txn.sf:hdr("Host")
|
||||||
|
local user_cn = txn:get_var("txn.xcn") or "XX"
|
||||||
local value = backends_map:lookup(key or "")
|
local value = backends_map:lookup(key or "")
|
||||||
if value ~= nil then
|
if value ~= nil then
|
||||||
|
local filtered_backends = {}
|
||||||
|
local all_backends = {}
|
||||||
local vals = utils.split(value, ",")
|
local vals = utils.split(value, ",")
|
||||||
-- todo: something smarter?
|
-- Single pass to filter and collect backends
|
||||||
return vals[math.random(#vals)]
|
for _, backend in ipairs(vals) do
|
||||||
else
|
local backend_server_name, server_cn = backend:match(server_cn_split_regex)
|
||||||
return ""
|
if backend_server_name then
|
||||||
|
table.insert(all_backends, backend_server_name)
|
||||||
|
if server_cn == user_cn or server_cn == "XX" then
|
||||||
|
table.insert(filtered_backends, backend_server_name)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Randomly select from filtered backends if available
|
||||||
|
if #filtered_backends > 0 then
|
||||||
|
return filtered_backends[math.random(#filtered_backends)]
|
||||||
|
elseif #all_backends > 0 then
|
||||||
|
-- If no filtered backends, randomly select from all backends
|
||||||
|
return all_backends[math.random(#all_backends)]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
core.register_fetches("get_server_names", get_server_names)
|
core.register_fetches("get_server_names", get_server_names)
|
||||||
|
@ -3,6 +3,8 @@ package.path = package.path .. "./?.lua;/etc/haproxy/scripts/?.lua;/etc/haproxy
|
|||||||
local pow_difficulty = tonumber(os.getenv("POW_DIFFICULTY") or 18)
|
local pow_difficulty = tonumber(os.getenv("POW_DIFFICULTY") or 18)
|
||||||
local backends_map = Map.new('/etc/haproxy/map/backends.map', Map._str)
|
local backends_map = Map.new('/etc/haproxy/map/backends.map', Map._str)
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
local server_cn_split_regex = "([^;]+);(%u%u)$"
|
||||||
|
local map_space_split_rexex = "([^%s]+)%s+([^%s]+)"
|
||||||
|
|
||||||
-- setup initial server backends based on hosts.map
|
-- setup initial server backends based on hosts.map
|
||||||
function setup_servers()
|
function setup_servers()
|
||||||
@ -23,21 +25,24 @@ function setup_servers()
|
|||||||
tcp:settimeout(10);
|
tcp:settimeout(10);
|
||||||
tcp:connect("127.0.0.1", 2000); --TODO: configurable port
|
tcp:connect("127.0.0.1", 2000); --TODO: configurable port
|
||||||
while line do
|
while line do
|
||||||
local domain, backend_host = line:match("([^%s]+)%s+([^%s]+)")
|
local domain, backend_data = line:match(map_space_split_rexex)
|
||||||
local new_map_value = server_prefix..counter
|
local backend_host, continent_code = backend_data:match(server_cn_split_regex)
|
||||||
|
local new_map_value = server_prefix .. counter .. ';' .. continent_code
|
||||||
local existing_map_value = backends_map:lookup(domain)
|
local existing_map_value = backends_map:lookup(domain)
|
||||||
if existing_map_value ~= nil then
|
if existing_map_value ~= nil then
|
||||||
current_backends = utils.split(existing_map_value, ",")
|
local current_backends = utils.split(existing_map_value, ",")
|
||||||
if not utils.contains(current_backends, new_map_value) then
|
if not utils.contains(current_backends, new_map_value) then
|
||||||
new_map_value = new_map_value .. "," .. existing_map_value
|
new_map_value = new_map_value .. "," .. existing_map_value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print("setting hosts.map "..domain.." "..new_map_value)
|
print("setting hosts.map " .. domain .. " " .. new_map_value)
|
||||||
core.set_map("/etc/haproxy/map/backends.map", domain, new_map_value)
|
core.set_map("/etc/haproxy/map/backends.map", domain, new_map_value)
|
||||||
local server_name = "servers/websrv"..counter
|
local server_name = "servers/websrv" .. counter
|
||||||
--NOTE: if you have a proper CA setup,
|
--NOTE: if you have a proper CA setup,
|
||||||
if verify_backend_ssl ~= nil then
|
if verify_backend_ssl ~= nil then
|
||||||
tcp:send(string.format("add server %s %s check ssl verify required ca-file ca-certificates.crt sni req.hdr(Host);", server_name, backend_host))
|
tcp:send(string.format(
|
||||||
|
"add server %s %s check ssl verify required ca-file ca-certificates.crt sni req.hdr(Host);",
|
||||||
|
server_name, backend_host))
|
||||||
else
|
else
|
||||||
tcp:send(string.format("add server %s %s;", server_name, backend_host))
|
tcp:send(string.format("add server %s %s;", server_name, backend_host))
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user