map $request_method $not_post {
	default 1;
	POST 0;
}

map $http_cache_control $opencpu_bypass_cache {
	"" 0;
	"no-cache" 1;
	default $not_post;
}

# Apache returns gzip for either deflate or gzip
map $http_accept_encoding $client_likes_gzip {
	default 0;
	"~gzip" 1;
	"~deflate" 1;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=opencpu:8m max_size=1000m inactive=1w;

server {
    listen 8000 default_server;
    listen [::]:8000 default_server;
    gzip_min_length 5000;

    location / {
        root /var/www/html/;
        gzip on;
        gzip_vary on;
        gzip_types text/plain text/css application/json application/x-javascript text/javascript application/javascript;
        autoindex off;

        add_header Pragma public;
        add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
    }

    location /phantasus/geo {
        gzip on;
        gzip_static on;
        gzip_vary on;
        gzip_types *;

        alias /var/phantasus/cache/geo/;
        try_files $uri =404;
        autoindex off;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
    }

    location /phantasus/preloaded {
        gzip on;
        gzip_static on;
        gzip_vary on;
        gzip_types *;

        alias /var/phantasus/cache/preloaded/;
        try_files $uri =404;
        autoindex off;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
    }

    location ~ /phantasus/ocpu/tmp/$ {
    	return 403;
    }

    location ~ /phantasus/ocpu/library/(?!phantasus) {
        return 403;
    }

    location /phantasus/ocpu {
        location ~ /phantasus/ocpu/library/phantasus/R/generatePreloadedSession/{
            return 403;
        }
        gzip on;
        gzip_types *;
        gzip_vary on;

        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://localhost:8001/phantasus/ocpu;
        proxy_redirect off;

        client_max_body_size 0;
        client_body_buffer_size 50k;
        proxy_connect_timeout       300;
        proxy_send_timeout          300;
        proxy_read_timeout          300;
        send_timeout                300;

        proxy_cache opencpu;
        proxy_cache_methods POST;
        proxy_cache_valid 400 404 5m;
        proxy_cache_valid 200 201 1w;
        proxy_cache_key "$request_method$request_uri$request_body";
        proxy_no_cache $request_body_file;
        proxy_buffers 8 512k;
        proxy_buffer_size 256k;
        proxy_ignore_headers Cache-Control;

        proxy_cache_bypass $request_body_file $opencpu_bypass_cache;
        proxy_no_cache $opencpu_bypass_cache;

        add_header X-ocpu-cache $upstream_cache_status;
    }
}
