Last active
April 21, 2023 13:09
-
-
Save derinbay/37726abc8030f3f3dff61b7cb75bd722 to your computer and use it in GitHub Desktop.
nginx.conf for selenium-grid hub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#user nobody; | |
worker_processes 1; | |
#error_log logs/error.log; | |
#error_log logs/error.log notice; | |
#error_log logs/error.log info; | |
#pid logs/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | |
# '$status $body_bytes_sent "$http_referer" ' | |
# '"$http_user_agent" "$http_x_forwarded_for"'; | |
#access_log logs/access.log main; | |
sendfile on; | |
#tcp_nopush on; | |
#keepalive_timeout 0; | |
keepalive_timeout 65; | |
#gzip on; | |
upstream seleniumhub { | |
server 10.2.80.7:4444; | |
server 10.2.80.10:4444; | |
} | |
server { | |
listen 80; | |
location ~ /wd/hub/session$ { | |
proxy_pass http://seleniumhub; | |
set $response_body ''; | |
# Reset the response's content_length, so that Lua can generate a | |
# body with a different length. | |
header_filter_by_lua_block { | |
ngx.header.content_length = nil | |
--ngx.log(ngx.ERR, " header filter ngx.var.upstream_addr ", ngx.var.upstream_addr) | |
} | |
body_filter_by_lua_block { | |
local resp_body = ngx.arg[1] | |
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body | |
if ngx.arg[2] then -- arg[2] is true if this is the last chunk | |
ngx.var.response_body = ngx.ctx.buffered | |
else | |
ngx.arg[1] = nil | |
return | |
end | |
-- extract the ip in number format from upstream host | |
local upstream_host = '127.0.0.1' | |
if ngx.var.upstream_addr then | |
local ngx_re = require "ngx.re" | |
local addrs = ngx_re.split(ngx.var.upstream_addr, ",") | |
if #addrs > 0 then | |
--ngx.log(ngx.ERR, "multiple upstream: " .. ngx.var.upstream_addr) | |
upstream_host = addrs[#addrs] | |
else | |
--ngx.log(ngx.ERR, "single upstream") | |
upstream_host = addrs | |
end | |
end | |
local o1,o2,o3,o4 = upstream_host:match("(%d+)%.(%d+)%.(%d+)%.(%d+)") -- this will exclude :4444 automatically | |
local num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4 | |
local ip = math.floor(num) | |
ngx.log(ngx.ERR, "current_response " .. ngx.var.response_body) | |
local current_session_id = ngx.var.response_body:match('"sessionId":"(%w+)') | |
local new_session_id = ip .. 'dddd' .. current_session_id | |
local modified_response = ngx.var.response_body:gsub(current_session_id, new_session_id) | |
ngx.log(ngx.ERR, "current_session_id " .. current_session_id .. "new id " .. new_session_id) | |
ngx.arg[1] = modified_response | |
} | |
} | |
location ~ /wd/hub/session/.+ { | |
set $target_host ''; | |
set $target_uri ''; | |
access_by_lua_block { | |
local url = ngx.var.request_uri | |
ngx.log(ngx.ERR, "url " .. url) | |
local session_id = url:match('/wd/hub/session/(%w+)') | |
local upstream_host_ip_in_num, upstream_session_id = session_id:match('(%w+)dddd(%w+)') | |
local o1 = math.floor(upstream_host_ip_in_num/(2^24)) | |
local rem = upstream_host_ip_in_num % (2^24) | |
local o2 = math.floor(rem/(2^16)) | |
rem = rem % (2^16) | |
local o3 = math.floor(rem/(2^8)) | |
rem = rem % (2^8) | |
local o4 = math.floor(rem) | |
local upstream_host_ip = o1 .. '.' .. o2 .. '.' .. o3 .. '.' .. o4 | |
ngx.var.target_host = upstream_host_ip | |
ngx.var.target_uri = url:gsub(session_id, upstream_session_id) | |
} | |
proxy_pass http://$target_host:4444$target_uri; | |
} | |
} | |
# HTTPS server | |
# | |
#server { | |
# listen 443 ssl; | |
# server_name localhost; | |
# ssl_certificate cert.pem; | |
# ssl_certificate_key cert.key; | |
# ssl_session_cache shared:SSL:1m; | |
# ssl_session_timeout 5m; | |
# ssl_ciphers HIGH:!aNULL:!MD5; | |
# ssl_prefer_server_ciphers on; | |
# location / { | |
# root html; | |
# index index.html index.htm; | |
# } | |
#} | |
include servers/*; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment