---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ $.Release.Name }}-nginx-cache
  labels:
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
data:
  nginx.conf: |
    user  nginx;
    worker_processes  2;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    #load_module /usr/lib/nginx/modules/ngx_http_ip2location_module.so;
    #load_module /usr/lib/nginx/modules/ngx_http_ip2proxy_module.so;

    events {
        use epoll;
        multi_accept on;
        worker_connections  10240;
    }
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        vhost_traffic_status_zone;
    
        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  main;
    
        sendfile        on;
        tcp_nopush     on;
    
        keepalive_timeout  65;
    
        gzip  on;

        proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:400m max_size=100g
                         inactive=30d use_temp_path=off;
    
        #ip2location_database /data/dbs/ip2location.bin;
        #ip2location_proxy_recursive	on;
        #ip2location_proxy	10.0.0.0/8;

        #ip2proxy_database /data/dbs/ip2proxy.bin;
        #ip2proxy_proxy_recursive on;
        #ip2proxy_proxy 10.0.0.0/8;

        server {
            listen *:10253;
            server_name _;

            vhost_traffic_status_bypass_limit on;
            vhost_traffic_status_bypass_stats on;

            location /health {
                access_log off;
                add_header 'Content-Type' 'text/plain';
                return 200 "healthy\n";
            }
        
            location /metrics {
                vhost_traffic_status_display;
                vhost_traffic_status_display_format prometheus;
            }
        }

        upstream origin_servers {
            {{- range $num, $ep := $.Values.endpoints }} 
            server {{ $ep }};
            {{- end }}
        }

        # URL shorter:
        # / --> /
        # /a --> /a
        # /a/b --> /a/*
        map $uri $shorten_url {
            ~^/$ /;
            ~^/([^/]+)$ /$1; 
            ~^/([^/]+)/.*$ /$1/*;
        }

        # URL shortener:
        # Example: / --> /
        # Example: /a --> /a
        # Example: /a/ --> /a
        # Example: /a/b --> /a/*
        map $uri $shorten_url {
            ~^/$ /;
            ~^/([^/]+)$ /$1;
            ~^/([^/]+)/$ /$1;
            ~^/([^/]+)/.*$ /$1/*;
        }

        server {
            listen *:80;
            server_name _;
            vhost_traffic_status_filter_by_host on;

            #vhost_traffic_status_filter_by_set_key $host country::$ip2location_country_short;
            vhost_traffic_status_filter_by_set_key $shorten_url url::$host;

            proxy_cache mycache;
            proxy_cache_revalidate on;
            proxy_cache_lock on;
            proxy_cache_key $scheme$http_host$request_uri;
            proxy_cache_purge PURGE from all;
            cache_purge_response_type json;
        
            proxy_cache_valid 200 1h;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

            proxy_cache_background_update on;

            proxy_connect_timeout 400ms;
            proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
        
            location / {
                proxy_set_header Host $http_host;

                ## debug
                add_header X-Cache-Status $upstream_cache_status;
                #add_header X-Cache-Node $hostname;
                #add_header X-Cache-Key $scheme$http_host$request_uri;

                proxy_set_header X-Real-IP $remote_addr;
                real_ip_header $real_ip_header;
                real_ip_recursive on;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                #proxy_set_header X-Anonymous-Type $ip2proxy_proxy_type;
                #proxy_set_header X-Country $ip2location_country_short;

                #proxy_set_header X-Country-Code $ip2location_country_short;
                #proxy_set_header X-Country-Name $ip2location_country_long;
                #proxy_set_header X-GeoIP-Region $ip2location_region;
                #proxy_set_header X-GeoIP-City $ip2location_city;
                #proxy_set_header X-Geoip-Country $ip2location_country_short;
                #proxy_set_header X-Geoip-Latitude $ip2location_latitude;
                #proxy_set_header X-Geoip-Longitude $ip2location_longitude;
                #proxy_set_header X-GeoIP-ISP $ip2location_isp;
                ##proxy_set_header X-GeoIP-Postal-Code $ip2location_zipcode;
                ##proxy_set_header X-Geoip-Timezone $ip2location_timezone;
                ##proxy_set_header X-Geoip-Asn $ip2location_asn;

                proxy_hide_header Pragma;
                proxy_hide_header Expires;

                # to backends
                proxy_pass http://origin_servers;
                proxy_buffering on;
            }
        }
    }
