Fixed nginx conf issue

This commit is contained in:
2024-08-08 14:30:22 -04:00
parent 07ff3a2e39
commit c49dcafc34
2 changed files with 98 additions and 136 deletions
+17 -34
View File
@@ -1,5 +1,5 @@
#user nobody; #user nobody;
worker_processes 8; worker_processes 4;
#error_log logs/error.log; #error_log logs/error.log;
#error_log logs/error.log notice; #error_log logs/error.log notice;
@@ -12,6 +12,7 @@ events {
worker_connections 1024; worker_connections 1024;
} }
http { http {
client_max_body_size 4g; client_max_body_size 4g;
@@ -23,22 +24,10 @@ http {
init_by_lua_block { init_by_lua_block {
auto_ssl = (require "resty.auto-ssl").new() auto_ssl = (require "resty.auto-ssl").new()
auto_ssl:set("storage_adapter", "resty.auto-ssl.storage_adapters.redis") auto_ssl:set("storage_adapter", "resty.auto-ssl.storage_adapters.redis")
auto_ssl:set("allow_domain", function(domain) auto_ssl:set("allow_domain", function(domain)
ngx.log(ngx.ERR, "!!!!!!!! nginx.conf allow_domain !!!!!!!!!! ", domain, ngx.ctx.toAllow)
return ngx.ctx.toAllow
end)
auto_ssl:set("request_domain", function(ssl, ssl_options)
local json = require "cjson" local json = require "cjson"
local redis = require "resty.redis"
local socket = assert(require "socket.unix"()) local socket = assert(require "socket.unix"())
local domain, err = ssl.server_name()
ngx.log(ngx.ERR, "!!!!!! nginx.conf request_domain !!!!!!!", domain)
local function connect(path) local function connect(path)
assert(socket:settimeout(.1)) assert(socket:settimeout(.1))
local status,err = pcall(function() assert(socket:connect(path)) end) local status,err = pcall(function() assert(socket:connect(path)) end)
@@ -47,21 +36,23 @@ http {
end end
if not domain then if not domain then
ngx.log(ngx.ERR, "no host header found") ngx.log(ngx.ERR, "no host header found")
ngx.exit(499) ngx.exit(499)
return false return false
end end
local redis = require "resty.redis"
local red = redis:new() local red = redis:new()
red:set_timeout(1000) -- 1 second red:set_timeout(1000) -- 1 second
local ok, err = red:connect("127.0.0.1", 6379) local ok, err = red:connect("127.0.0.1", 6379)
if not ok then if not ok then
ngx.log(ngx.ERR, "failed to connect to redis: ", err) ngx.log(ngx.ERR, "failed to connect to redis: ", err)
return ngx.exit(598) return ngx.exit(598)
end end
local res, err = red:hgetall("proxy_Host_"..domain) local res, err = red:hgetall("proxy_host_"..domain)
local res = red:array_to_hash(res) local res = red:array_to_hash(res)
if not res["ip"] then if not res["ip"] then
@@ -79,21 +70,13 @@ http {
end end
if not res["ip"] then if not res["ip"] then
ngx.say('The domain is not allowed on this server.') ngx.log(ngx.ERR, "no host found for key ", domain)
ngx.exit(406) -- ngx.exit(406)
return false return false
end end
ngx.ctx.targetInfo = res return true
ngx.ctx.toAllow = true;
if res['wildcard_parent'] then
return res['wildcard_parent'], err
end
return domain, err
end) end)
auto_ssl:init() auto_ssl:init()
} }
@@ -135,7 +118,7 @@ http {
#keepalive_timeout 0; #keepalive_timeout 0;
keepalive_timeout 65; keepalive_timeout 65;
gzip on; #gzip on;
include sites-enabled/*; include sites-enabled/*;
} }
+81 -102
View File
@@ -14,124 +14,103 @@ server {
location / { location / {
set $target ''; set $target '';
set $target_scheme 'http'; set $target_scheme 'http';
set $target_port ''; set $target_port '';
set $header_host $host; set $header_host $host;
access_by_lua { access_by_lua '
local host = ngx.var.host
local uri = ngx.var.uri
local scheme = ngx.var.scheme
function getTargetInfo(domain, targetInfo) local json = require "cjson"
if targetInfo then local socket = assert(require "socket.unix"())
ngx.log(ngx.ERR, "!!!!!!! getTargetInfo targetFound") local function connect(path)
assert(socket:settimeout(.1))
local status,err = pcall(function() assert(socket:connect(path)) end)
if status then return true end
return false
end
return targetInfo if not host then
end ngx.log(ngx.ERR, "no host header found")
return ngx.exit(499)
end
local json = require "cjson" local redis = require "resty.redis"
local redis = require "resty.redis" local red = redis:new()
local socket = assert(require "socket.unix"())
local domain, err = ssl.server_name() red:set_timeout(1000) -- 1 second
ngx.log(ngx.ERR, "!!!!!! nginx.conf request_domain !!!!!!!", domain) local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
local function connect(path) ngx.log(ngx.ERR, "failed to connect to redis: ", err)
assert(socket:settimeout(.1)) return ngx.exit(598)
local status,err = pcall(function() assert(socket:connect(path)) end) end
if status then return true end
return false
end
if not domain then local res, err = red:hgetall("proxy_host_"..host)
ngx.log(ngx.ERR, "no host header found") local res = red:array_to_hash(res)
ngx.exit(499)
return false
end
local red = redis:new() if not res["ip"] then
red:set_timeout(1000) -- 1 second if connect("/var/run/proxy_lookup.socket") then
assert(socket:send(json.encode({domain = host})))
while 1 do
local s, status, partial = socket:receive()
if partial then
res = json.decode(partial)
socket:close()
break
end
end
end
end
local ok, err = red:connect("127.0.0.1", 6379) if not res["ip"] then
if not ok then ngx.log(ngx.ERR, "no host found for key ", host)
ngx.log(ngx.ERR, "failed to connect to redis: ", err) return ngx.exit(406)
return ngx.exit(598) end
end
local res, err = red:hgetall("proxy_Host_"..domain) if scheme == "http" then
local res = red:array_to_hash(res) if res["forcessl"] == "true" then
return ngx.redirect("https://"..host..uri, 301)
end
end
if not res["ip"] then if res["targetssl"] == "true" then
if connect("/var/run/proxy_lookup.socket") then ngx.var.target_scheme = "https"
assert(socket:send(json.encode({domain = domain}))) end
while 1 do
local s, status, partial = socket:receive()
if partial then
res = json.decode(partial)
socket:close()
break
end
end
end
end
if not res["ip"] then if res["host-pass-though"] == "false" then
ngx.say('The domain is not allowed on this server.') ngx.var.header_host = res["ip"]
ngx.exit(406) end
return false
end ngx.var.target = res["ip"]
ngx.var.target_port = res["targetPort"]
ngx.ctx.targetInfo = res ';
ngx.ctx.toAllow = true;
end
local host = ngx.var.host resolver 192.168.1.1 ipv6=off; #8.8.4.4; # use Google's open DNS server
local uri = ngx.var.uri
local scheme = ngx.var.scheme
local res = getTargetInfo(host, ngx.ctx.targetInfo)
proxy_http_version 1.1;
proxy_pass_request_headers on;
proxy_pass $target_scheme://$target:$target_port;
if scheme == "http" then proxy_set_header Upgrade $http_upgrade;
if res["forcessl"] == "true" then proxy_set_header Connection $connection_upgrade;
return ngx.redirect("https://"..host..uri, 301) proxy_ssl_session_reuse on;
end proxy_intercept_errors off;
end
if res["targetssl"] == "true" then proxy_set_header Host $header_host;
ngx.var.target_scheme = "https" add_header X-Target-Host $target;
end proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $target_scheme;
proxy_set_header Referer $target_scheme://$header_host;
proxy_set_header Accept-Language $http_accept_language;
proxy_set_header User-Agent $http_user_agent;
if res["host-pass-though"] == "false" then sub_filter $target $host;
ngx.var.header_host = res["ip"] sub_filter_once off;
end
ngx.var.target = res["ip"]
ngx.var.target_port = res["targetPort"]
}
resolver 192.168.1.1 ipv6=off; #8.8.4.4; # use Google's open DNS server
proxy_http_version 1.1;
proxy_pass_request_headers on;
proxy_pass $target_scheme://$target:$target_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_ssl_session_reuse on;
proxy_intercept_errors off;
proxy_set_header Host $header_host;
add_header X-Target-Host $target;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $target_scheme;
proxy_set_header Referer $target_scheme://$header_host;
proxy_set_header Accept-Language $http_accept_language;
proxy_set_header User-Agent $http_user_agent;
sub_filter $target $host;
sub_filter_once off;
} }
} }