upstream chm_manager { server chmmanager:8080; }
upstream chm_server { server chmsoftware:8080; }

server {
    listen 80 default_server;
    location /Viewer {
        proxy_pass http://chm_server/Viewer;
        proxy_redirect http://chm_server/Viewer /Viewer;
	proxy_set_header Host            $http_host;
	proxy_set_header Referer         "";
	proxy_set_header X-Real-IP       $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /Builder {
        proxy_pass http://chm_server/Builder;
        proxy_redirect http://chm_server/Builder /Builder;
	proxy_set_header Host            $http_host;
	proxy_set_header Referer         "";
	proxy_set_header X-Real-IP       $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ~* /config.txt$ {
        root /usr/share/nginx/html;
        sub_filter MYHOST "$host";
	sub_filter_types "*";
	sub_filter_once off;
    }
    location / {
        root /usr/share/nginx/html;
    }

}

server {
    listen 443 ssl default_server;
    large_client_header_buffers 4 64000;
    ssl_certificate /etc/nginx/conf.d/nginx.crt;
    ssl_certificate_key /etc/nginx/conf.d/nginx.key;
    ssl_client_certificate /etc/nginx/conf.d/client_certificates.pem;
    ssl_trusted_certificate /etc/nginx/conf.d/nginx.pem;
    ssl_verify_client optional;
    ssl_verify_depth 2;

    location /Manager/ {
	auth_basic "Restricted access";
	auth_basic_user_file /etc/nginx/conf.d/passwdfile;
        proxy_pass http://chm_manager/NGCHMManager/;
        proxy_redirect http://chm_manager/Manager/ /NGCHMManager/;
	proxy_set_header Host            $http_host;
	proxy_set_header Referer         "";
	proxy_set_header X-Real-IP       $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	client_max_body_size		 100M;
    }
    location /Manager {
	return 301 http://$http_host/Manager/index.html;
    }
    location /ManagerAPI/ {
        if ($ssl_client_verify != SUCCESS) {
	    return 403;
	}
        proxy_pass http://chm_manager/NGCHMManager/;
        proxy_redirect http://chm_manager/Manager/ /NGCHMManager/;
	proxy_set_header Host            $http_host;
	proxy_set_header Referer         "";
	proxy_set_header X-Real-IP       $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	client_max_body_size		 100M;
    }
    location /ManagerAPI {
	return 301 http://$http_host/ManagerAPI/index.html;
    }
}
