user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    proxy_cache_path /tmp/maps-osm/ levels=1:2 keys_zone=osm_cache:100m max_size=10g inactive=60d;
    proxy_cache_path /tmp/maps-satellite/ levels=1:2 keys_zone=satellite_cache:100m max_size=9g inactive=90d;
    proxy_cache_path /tmp/maps-nexrad/ levels=1:2 keys_zone=nexrad_cache:10m max_size=1g inactive=1h;

    server {
        listen 80;
        server_name _;

        location ~ ^/osm/tiles/ {
            rewrite ^/osm/tiles/(.*)$ /styles/v1/mapbox/streets-v11/tiles/$1?access_token=MAPBOX-ACCESS-TOKEN-HERE break;
            proxy_ssl_server_name on;
            proxy_pass https://api.mapbox.com;
            proxy_cache osm_cache;
            proxy_cache_valid 200 302 304 30d;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_background_update on;
            add_header X-Proxy-Cache $upstream_cache_status;
            expires 30d;
        }

        location ~ ^/satellite/tiles/ {
            rewrite ^/satellite/tiles/(.*)$ /styles/v1/mapbox/satellite-v9/tiles/$1?access_token=MAPBOX-ACCESS-TOKEN-HERE break;
            proxy_ssl_server_name on;
            proxy_pass https://api.mapbox.com;
            proxy_cache satellite_cache;
            proxy_cache_valid 200 302 304 90d;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_background_update on;
            add_header X-Proxy-Cache $upstream_cache_status;
            expires 30d;
        }

        location ~ ^/nexrad/ {
            rewrite ^/nexrad/(.*)$ /cgi-bin/wms/nexrad/n0q.cgi$is_args$args break;
            proxy_ssl_server_name on;
            proxy_pass http://mesonet.agron.iastate.edu;
            proxy_cache nexrad_cache;
            proxy_cache_valid 200 302 304 5m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_background_update on;
            add_header X-Proxy-Cache $upstream_cache_status;
            expires 30d;
        }

        location / {
            root /var/www/;
            index index.html index.php;

            error_page 404 = /index.php;
            try_files $uri $uri/ index.php =404;

            location ~ \.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            }
        }
    }
}