server {
    if ($host = $NGINX_SERVER_NAME) {
        return 301 https://$host$request_uri;
    }

    server_name $NGINX_SERVER_NAME;
    listen 80;
    listen [::]:80;
    return 404;
}

server {
    server_name $NGINX_SERVER_NAME;

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    ssl_certificate $NGINX_SSL_CERTIFICATE;
    ssl_certificate_key $NGINX_SSL_CERTIFICATE_KEY;
    include $NGINX_SSL_INCLUDE;
    ssl_dhparam $NGINX_SSL_DHPARAM;

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

    #gzip_static  on;
    gzip on;
    gzip_proxied any;
    gzip_types
        text/css
        text/javascript
        text/xml
        text/plain
        application/javascript
        application/x-javascript
        application/json;

    # Legacy URLS
    set $REWRITE_LEGACY_URLS "$NGINX_REWRITE_LEGACY_URLS";
    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://127.0.0.1:3000;
		proxy_redirect     off;

        access_log $GRADIDO_LOG_PATH/nginx-access.frontend.log gradido_log;
        error_log $GRADIDO_LOG_PATH/nginx-error.frontend.log warn;
	}

    # 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://127.0.0.1:4000;
        proxy_redirect     off;

        access_log $GRADIDO_LOG_PATH/nginx-access.backend.log gradido_log;
        error_log $GRADIDO_LOG_PATH/nginx-error.backend.log warn;
    }

    # 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://127.0.0.1:4000/hook;
        proxy_redirect     off;

        access_log $GRADIDO_LOG_PATH/nginx-access.backend.hook.log gradido_log;
        error_log $GRADIDO_LOG_PATH/nginx-error.backend.hook.log warn;
    }

    # Webhook reverse proxy
    location /hooks/ {
        proxy_pass http://127.0.0.1:9000/hooks/;

        access_log $GRADIDO_LOG_PATH/nginx-access.hooks.log gradido_log;
        error_log $GRADIDO_LOG_PATH/nginx-error.hooks.log warn;
    }

    # 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;
        
        proxy_pass         http://127.0.0.1:8080/;
        proxy_redirect     off;

        access_log $GRADIDO_LOG_PATH/nginx-access.admin.log gradido_log;
        error_log $GRADIDO_LOG_PATH/nginx-error.admin.log warn;
    }
    
    # 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;
    #}
}