worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

daemon off;

events {
  worker_connections 1024;
  debug_connection ::1/128;
}

http {
  default_type application/octet-stream;
  keepalive_timeout 65;
  log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" "$upstream_connect_time" "$upstream_response_time" "$upstream_status"';
  access_log /var/log/nginx/access.log  main;
  sendfile on;
  proxy_cache_use_stale timeout updating http_500 http_503 http_504;
  proxy_cache_background_update on;
  proxy_connect_timeout 10s;
  proxy_intercept_errors on;
  proxy_cache_bypass $http_x_refresh;
  proxy_cache_path /tmp/package_cache/md use_temp_path=off keys_zone=mdcache:10m inactive=1d max_size=10g;
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
  map $upstream_http_content_length $nocache {
    "default" 1;
    "~^0$" 0;
  }
  root /home/user/nginx/;

  upstream backend_main {
    hash 'balance';
    server [::1]:81 fail_timeout=10;
  }
  upstream backend_fallback {
    hash 'balance';
    server [::1]:82 fail_timeout=60;
  }
  recursive_error_pages on;
  server {
    listen [::]:88;
    server_name _;
    add_header X-Test 'qwe-88';
    location / {
      proxy_pass http://backend_main;
      proxy_cache mdcache;
      proxy_cache_valid 1m;
      add_header X-Header-Root '88-/';
      add_header Cache-Control "max-age=88" always;
      error_page 500 502 503 504 =  @fallback;
   }
    location /url {
      proxy_pass http://backend_main;
      proxy_cache mdcache;
      proxy_cache_valid 1m;
      add_header X-Header-Root '88-/url';
      add_header Cache-Control "max-age=888" always;
      error_page 500 502 503 504 =  @fallback;
   }
    location @fallback {
      proxy_pass http://[::]:89;
      add_header X-Header-Fall '/fallback';
    }
  }
  server {
    listen [::]:89;
    server_name _;
    add_header X-Test 'qwe-89';
    location / {
      proxy_pass http://backend_fallback;
      proxy_cache mdcache;
      proxy_cache_valid 1m;
      add_header X-Header-Root '89-/';
      add_header Cache-Control "max-age=89" always;
   }
    location /url {
      proxy_pass http://backend_fallback;
      proxy_cache mdcache;
      proxy_cache_valid 1m;
      add_header X-Header-Root '89-/url';
      add_header Cache-Control "max-age=899" always;
   }
  }
  server {
    listen [::]:81;
    server_name _;
    add_header X-Backend '81';
    location / {
      if (-f $document_root/81){
        return 502 "Bad 81";
      }
      return 200 'Good 81';
      add_header Cache-Control "max-age=81" always;
    }
  }
  server {
    listen [::]:82;
    server_name _;
    add_header X-Backend '82';
    location / {
      if (-f $document_root/82){
        return 502 "Bad 82";
      }
      return 200 'Good 82';
      add_header Cache-Control "max-age=82" always;
    }
  }
}
