user nginxuser;
worker_processes auto;
pid /run/nginx.pid;

error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;


events {
	worker_connections 1024;
	# multi_accept on;
}

stream {
	limit_conn_zone $binary_remote_addr zone=whois_conn:10m;

    upstream whois_backend {
        server 127.0.0.1:4300;
    }

    server {
        listen 4301;
        limit_conn whois_conn 1;
	proxy_pass whois_backend;
        proxy_connect_timeout 5s;
        proxy_timeout 30s;        # idle timeout for the proxied connection
    }
}

http {
# 1. Mark whitelisted CIDRs. geo matches IPs/ranges; built-in module, no install needed.
geo $rate_whitelist {
    default        0;
    185.17.237.0/24 1; # cocca trusted range Telehouse
    185.17.236.0/24 1; # cocca trusted range Aqua Ray
    185.17.238.0/24 1; # cocca trusted range Dublin
    185.17.239.0/24 1; # cocca trusted range Dublin
}

# 2. Build the limit key: empty for whitelisted (skips limiting), IP otherwise.
map $rate_whitelist $limit_key {
    0  $binary_remote_addr;   # not whitelisted -> rate limited per IP
    1  "";                    # whitelisted     -> empty key -> NOT limited
}

# 3. Zones now key on $limit_key instead of $binary_remote_addr directly.
limit_req_zone  $limit_key   zone=perip:20m  rate=5r/s;
limit_conn_zone $limit_key   zone=conn:20m;

# global ceiling — see note below about whether to whitelist it
limit_req_zone  $server_name zone=global:10m rate=100r/s;

	##
	# Basic Settings
	##
	        
        proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=static_cache:10m max_size=250m inactive=60m use_temp_path=off;
	proxy_cache_key "$scheme$request_method$host$request_uri";

	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##
	ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	##access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log warn;
        access_log /var/log/nginx/access.log combined buffer=32k;

	map $request_uri $log_healthcheck {
	default 1;
	~^/healthz$ 0;
	}

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

