# 
# nginx userland template
# this configuration is designed for running nginx as the omero user or similar
# nginx -c etc/nginx.conf
# for inclusion in a system-wide nginx configuration see omero web config nginx
#
pid %(ROOT)s/var/pid.nginx;
error_log %(ROOT)s/var/log/nginx_error.log;
worker_processes  5;
working_directory %(ROOT)s/var;

events {
    worker_connections  1024;
}


http {
    access_log    %(ROOT)s/var/log/nginx_access.log;
    include       %(ROOT)s/etc/mime.types;
    default_type  application/octet-stream;
    client_body_temp_path %(ROOT)s/var/nginx_tmp;

    keepalive_timeout 65;

    upstream omeroweb%(PREFIX_NAME)s {
        server %(FASTCGI_EXTERNAL)s fail_timeout=0;
    }
    
    server {
        listen %(HTTPPORT)d;
        server_name _;
        proxy_temp_path %(ROOT)s/var/nginx_tmp;

        sendfile on;
        client_max_body_size %(MAX_BODY_SIZE)s;

%(NGINX_SERVER_EXTRA_CONFIG)s

        # maintenance page serve from here
        location @maintenance%(PREFIX_NAME)s {
            root %(ROOT)s/etc/templates/error;
            try_files $uri /maintainance.html =502;
        }

        # weblitz django apps serve media from here
        location %(STATIC_URL)s {
            alias %(STATIC_ROOT)s;
        }

        location @proxy_to_app%(PREFIX_NAME)s {
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://omeroweb%(PREFIX_NAME)s;
        }

        location %(FORCE_SCRIPT_NAME)s {

            error_page 502 @maintenance%(PREFIX_NAME)s;
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app%(PREFIX_NAME)s;
        }

    }

}
