server {
    server_name $NGINX_SERVER_NAME;

    listen 80;
    listen [::]:80;

    #include /etc/nginx/common/protect.conf;
    #include /etc/nginx/common/protect_add_header.conf;
	#include /etc/nginx/common/ssl.conf;

    #gzip_static  on;

    # Legacy URLS
    set $REWRITE_LEGACY_URLS "true";
    if ($REWRITE_LEGACY_URLS = 'true') {
        rewrite ^/vue/?(.*)$ /$1 permanent;
    }

    # Frontend (default)
    location / {
		proxy_http_version 1.1;
		proxy_set_header   Upgrade $http_upgrade;
		proxy_set_header   Connection 'upgrade';
		proxy_set_header   X-Forwarded-For $remote_addr;
		proxy_set_header   X-Real-IP  $remote_addr;
		proxy_set_header   Host $host;
		
		proxy_pass         http://frontend:3000;
		proxy_redirect     off;
	}

    # Backend
    location /graphql {
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection 'upgrade';
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   Host $host;
        
        proxy_pass         http://backend:4000;
        proxy_redirect     off;
    }

    # Backend webhooks
    location /hook {
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection 'upgrade';
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   Host $host;
        
        proxy_pass         http://backend:4000/hook;
        proxy_redirect     off;
    }

    # Admin Frontend
    location /admin {
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection 'upgrade';
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   Host $host;
      
        # TODO: in docker environemnt we do not have the trailing slash. This needs work
        proxy_pass         http://admin:8080;
        proxy_redirect     off;
    }

    # TODO this could be a performance optimization
    #location /vue {
    #    alias /var/www/html/gradido/frontend/dist;
    #    index index.html;
    #
    #    location ~* \.(png)$ {
    #        expires 39d;
    #    }
    #    try_files $uri $uri/ /index.html = 404;
    #}

    #access_log /var/log/nginx/access.log main;
}