upstream frontend {
    server frontend:${FRONTEND_PORT};
}

upstream api {
    server api:${API_PORT};
}

upstream authenticator {
    server authenticator:${AUTHENTICATOR_PORT};
}

upstream pdfgenerator {
    server pdfgenerator:${PDFGENERATOR_PORT};
}

server {
    listen ${NGINX_PORT};

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    location /app {
        proxy_pass http://frontend;
    }

    location ^~ /api/v2/ {
        proxy_pass http://api;
    }

    location /api/v2/authenticator {
        rewrite /api/v2/authenticator/(.*) /$1  break;
        proxy_pass http://authenticator;
    }

    location ^~ /api/v2/templates {
        rewrite /api/v2/(.*) /pdfgenerator/$1  break;
        proxy_pass http://pdfgenerator;
    }

    location ^~ /api/v2/documents {
        rewrite /api/v2/(.*) /pdfgenerator/$1  break;
        proxy_pass http://pdfgenerator;
    }
}
