任务:nginx使用dynamic查找和caching为反向代理configuration泊坞窗容器。
处理stream程:
当前configuration(模板):
server { listen *; server_name {{host}}; set $attempt 0; location / { try_files '/dev/null' @run; } location @run { internal; set $container_name "{{container_name}}"; set $upstream ""; rewrite_by_lua ' local attempt = tonumber(ngx.var.attempt) if attempt > 1 then ngx.log(ngx.ALERT, "Upstream down") ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end local routes = ngx.shared.upstream_cache if attempt > 0 then ngx.log(ngx.ALERT, "Deleteing cache") routes:delete(ngx.var.http_host) end ngx.var.attempt = attempt + 1 -- try cached route first local route = routes:get(ngx.var.http_host) if route == nil then ngx.log(ngx.ALERT, "Asking docker about IP of " .. ngx.var.http_host) local handle = io.popen("docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' " .. ngx.var.container_name) local result = handle:read("*a") handle:close() route = result end if route ~= nil then ngx.var.upstream = route:gsub("^%s*(.-)%s*$", "%1") routes:set(ngx.var.http_host, route) else ngx.exit(ngx.HTTP_NOT_FOUND) end '; error_page 504 @run; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_send_timeout 30; proxy_read_timeout 30; proxy_connect_timeout 2; proxy_pass $scheme://$upstream; } }
它几乎工作。 除非caching无效并且error_page触发,否则一切都可以。
在这种情况下,该进程按照它应该运行,这是看到日志:
[error] 7238#0: *6 upstream timed out (110: Connection timed out) while connecting to upstream [alert] 7238#0: *6 [lua] [string "rewrite_by_lua"]:12: Deleteing cache [alert] 7238#0: *6 [lua] [string "rewrite_by_lua"]:21: Asking docker about IP
它向上游提出了正确的要求。
但反应是空的!
在下一个请求 – 一切正常,上游取自caching。
为什么以及如何解决这个问题?
该死的简单。 error_page 504 = @run; 而不是error_page 504 @run;