# Default before peer relation is handled
proxy_cache_path /mnt/ramdisk/proxy-cache levels=1:2 keys_zone=proxycache:5m max_size=1000m;

upstream backend {
  server 127.0.0.1:8080;
}

server {
  listen 80 default;
  server_name _;
  location / {
    set $no_cache "";

    if ($request_method !~ ^(GET|HEAD)$) {
      set $no_cache "1";
    }

    if ($no_cache = "1") {
      add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
      add_header X-Microcachable "0";
    }

    if ($http_cookie ~* "_mcnc") {
      set $no_cache "1";
    }

    proxy_no_cache $no_cache;
    proxy_cache_bypass $no_cache;

    proxy_redirect   http://backend  /;
    proxy_pass  http://backend;
    proxy_cache proxycache;
    proxy_cache_key $scheme$host$request_method$request_uri;
    proxy_cache_valid 200 60s;
    proxy_cache_use_stale updating;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_max_temp_file_size 1M;

    # Custom logging
    log_format custom '$remote_addr - $remote_user [$time_local]  '
             '"$request" $status $body_bytes_sent '
             '"$http_referer" "$http_user_agent" nocache:$no_cache';
    access_log  /mnt/logs/microcache.log custom;

  }
}

