map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    "application/javascript"    365d;
    "text/css"                  365d;
    "text/css; charset=utf-8"   365d;
    default                     off;
}

map $args $skip_static {
    "" 0;
    default 1;
}

upstream app {
    server app:8080;
}

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    client_max_body_size 20M;

    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    location / {
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|woff|woff2|eot|txt)$ {
            try_files $uri $uri/ =404;
            expires 365d;
        }
        location /robots.txt {
            try_files /robots.txt @fallback;
            gzip_static on;
        }
        location / {
            proxy_cache local_cache;
            proxy_cache_valid 200 1h;
            proxy_cache_lock on;

            #proxy_cache_bypass $no_cache;
            #proxy_no_cache $no_cache;

            add_header X-Proxy-Cache $upstream_cache_status;

            proxy_ignore_headers Cache-Control Set-Cookie Expires;

            proxy_buffers 16 16k;
            proxy_buffer_size 32k;

            if ($args) {
                proxy_pass http://app;
                break;
            }

            try_files $uri $uri/ @fallback;
            gzip_static on;
        }
    }

    location @fallback {
        expires $expires;
        proxy_redirect off;
        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_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 1m;
        proxy_connect_timeout 1m;
        proxy_pass http://app;
    }
}
