# ERB templated nginx configuration
# see https://doc.scalingo.com/platform/deployment/buildpacks/nginx

upstream backend_server {
	server localhost:8000 fail_timeout=0;
}

server {
	
	listen <%= ENV["PORT"] %>;
	server_name _;

    root /app/build/frontend-out;
    
    error_page 404 /404.html;

    # Django rest framework and external API
    location ~ ^/(api|external_api)/ {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect off;
        proxy_pass http://backend_server;
    }

    # Django admin
    location ^~ /admin/ {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect off;
		proxy_pass http://backend_server;
	}

    location ~ "^/401/?$" {
        try_files $uri /401.html;
    }

    location ~ "^/403/?$" {
        try_files $uri /403.html;
    }
    
    location = /404.html {
        internal;
    }

    # Frontend export
    location / {
        try_files $uri index.html $uri/ =404;
    }

}
