upstream philomena {
	server app:4000 fail_timeout=0;
}

server {
	listen 80 default;
	listen [::]:80;

	root $APP_DIR/priv/static;

	client_max_body_size       125000000;
	client_body_buffer_size    128k;

	location ~ ^/img/view/(.+)/([0-9]+).*\.([A-Za-z0-9]+)$ {
		expires max;
		add_header      Cache-Control public;
		alias "$APP_DIR/priv/static/system/images/thumbs/$1/$2/full.$3";
	}

	location ~ ^/img/download/(.+)/([0-9]+).*\.([A-Za-z0-9]+)$ {
		add_header Content-Disposition "attachment";
		expires max;
		add_header      Cache-Control public;
		alias "$APP_DIR/priv/static/system/images/thumbs/$1/$2/full.$3";
	}

	location ~ ^/img/(.+)   {
		expires max;
		add_header Cache-Control public;
		alias $APP_DIR/priv/static/system/images/thumbs/$1;
	}

	location ~ ^/spns/(.+)   {
		expires max;
		add_header Cache-Control public;
		alias $APP_DIR/priv/static/system/images/adverts/$1;
	}

	location ~ ^/avatars/(.+)   {
		expires max;
		add_header Cache-Control public;
		alias $APP_DIR/priv/static/system/images/avatars/$1;
	}

	location ~ ^/media/(.+)   {
		expires max;
		add_header Cache-Control public;
		alias $APP_DIR/priv/static/system/images/$1;
	}

	location / {
		try_files $uri @proxy;
	}

	location @proxy {
		proxy_pass         http://philomena;
		proxy_redirect     off;

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

		proxy_connect_timeout      90;
		proxy_send_timeout         90;
		proxy_read_timeout         90;

		proxy_buffer_size          4k;
		proxy_buffers              4 32k;
		proxy_busy_buffers_size    64k;
		proxy_temp_file_write_size 64k;

		# Configuration for Phoenix WS
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
	}
}