upstream sapl_server {
  server unix:/var/interlegis/sapl/run/gunicorn.sock fail_timeout=0;
}
upstream channels_server {
  server unix:/var/interlegis/sapl/run/daphne.sock;
}

server {
    listen   80;
    server_name sapl.test;
    client_max_body_size 4G;

    location /static/ {
        alias /var/interlegis/sapl/collected_static/;
    }

    location /media/ {
        alias /var/interlegis/sapl/media/;
    }

    location / {
        try_files $uri @proxy_to_app;
    }

    location /ws/ {
        try_files $uri @proxyto_ws;
    }

    location @proxyto_ws {
        proxy_pass http://channels_server;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        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-Host $server_name;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $http_host;

        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://sapl_server;
            break;
        }
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /var/interlegis/sapl/sapl/static/;
    }
}