worker_processes 4;

events { worker_connections 1024; }

http {
    include mime.types;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    charset utf-8;
    charset_types text/plain text/css application/json text/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;

    gzip  on;
    gzip_static  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css application/json text/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    upstream pandora-web {
        server pandora:2620;
    }

    upstream pandora-websocket {
        server websocketd:2622;
    }

    server {

        listen   80 default;

        access_log  off;
        error_log   /var/log/nginx/error.log;

        location /favicon.ico {
            root   /pandora/static;
        }

        location /static/ {
            root   /pandora;
            autoindex off;
        }
        location /data/ {
            internal;
            root   /pandora;
        }

        location /api/ws/ {
                proxy_http_version 1.1;
                proxy_set_header Host $http_host;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Proxy "";
                proxy_redirect off;
                proxy_buffering off;
                proxy_read_timeout 999999999;
                proxy_pass http://pandora-websocket/;
        }

        location / {
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header X-Forwarded-Proto http;
                proxy_set_header Host $http_host;
                proxy_set_header Proxy "";
                proxy_redirect off;
                proxy_buffering off;
                proxy_read_timeout 90;    #should be in sync with gunicorn timeout
                proxy_connect_timeout 90; #should be in sync with gunicorn timeout
                if (!-f $request_filename) {
                    proxy_pass http://pandora-web;
                    break;
                }
                client_max_body_size 32m;
        }

        error_page  400  /;
        error_page  404  /404.html;
        location  /404.html {
            root   /pandora/static/html;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location  /50x.html {
            root   /pandora/static/html;
        }
    }
}