Files
proxy/ops/nginx_conf/nginx.conf
T
2024-08-08 09:15:56 -04:00

142 lines
3.8 KiB
Nginx Configuration File

#user nobody;
worker_processes 8;
#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 {
client_max_body_size 4g;
lua_shared_dict auto_ssl 100m;
lua_shared_dict auto_ssl_settings 64k;
resolver 8.8.4.4 8.8.8.8;
init_by_lua_block {
auto_ssl = (require "resty.auto-ssl").new()
auto_ssl:set("storage_adapter", "resty.auto-ssl.storage_adapters.redis")
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 redis = require "resty.redis"
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)
assert(socket:settimeout(.1))
local status,err = pcall(function() assert(socket:connect(path)) end)
if status then return true end
return false
end
if not domain then
ngx.log(ngx.ERR, "no host header found")
ngx.exit(499)
return false
end
local red = redis:new()
red:set_timeout(1000) -- 1 second
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.log(ngx.ERR, "failed to connect to redis: ", err)
return ngx.exit(598)
end
local res, err = red:hgetall("proxy_Host_"..domain)
local res = red:array_to_hash(res)
if not res["ip"] then
if connect("/var/run/proxy_lookup.socket") then
assert(socket:send(json.encode({domain = domain})))
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
ngx.say('The domain is not allowed on this server.')
ngx.exit(406)
return false
end
ngx.ctx.targetInfo = res
ngx.ctx.toAllow = true;
if res['wildcard_parent'] then
return res['wildcard_parent'], err
end
return domain, err
end)
auto_ssl:init()
}
init_worker_by_lua_block {
auto_ssl:init_worker()
}
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server {
listen 127.0.0.1:8999;
# Increase the body buffer size, to ensure the internal POSTs can always
# parse the full POST contents into memory.
client_body_buffer_size 128k;
client_max_body_size 128k;
location / {
content_by_lua_block {
auto_ssl:hook_server()
}
}
}
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 /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
include sites-enabled/*;
}