upstream django_backend {
    server 127.0.0.1:8000;
}

upstream prom_backend {
    server 127.0.0.1:9090;
}

server {
    listen 443 ssl http2;
    server_name api.evibes.com b2b.evibes.com;
    ssl_certificate     /etc/letsencrypt/live/evibes.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/evibes.com/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;
    ssl_session_cache   shared:SSL:10m;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header X-Frame-Options         SAMEORIGIN                 always;
    add_header X-Content-Type-Options  nosniff                    always;
    add_header Referrer-Policy         no-referrer-when-downgrade always;
    client_max_body_size 100M;

    location / {
        proxy_pass         http://django_backend;
        proxy_set_header   Host               $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 /favicon.ico {
        add_header Access-Control-Allow-Origin "*";
        root /var/jenkins/workspace/evibes/static;
    }

    location = /robots.txt {
        add_header Access-Control-Allow-Origin "*";
        alias            /var/jenkins/workspace/evibes/static/robots_backend.txt;
        default_type     text/plain;
    }

    location /media/ {
        add_header Access-Control-Allow-Origin "*";
        root             /var/jenkins/workspace/evibes;
    }

    location /static/ {
        add_header Access-Control-Allow-Origin "*";
        root             /var/jenkins/workspace/evibes;
    }

    error_page 500 502 503 504 /maintenance.html;
    location = /maintenance.html {
        add_header Access-Control-Allow-Origin "*";
        root             /var/jenkins/workspace/evibes/static;
        internal;
    }
}

server {
    listen 443 ssl http2;
    server_name evibes.com www.evibes.com;
    ssl_certificate     /etc/letsencrypt/live/evibes.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/evibes.com/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;
    ssl_session_cache   shared:SSL:10m;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header X-Frame-Options         SAMEORIGIN                 always;
    add_header X-Content-Type-Options  nosniff                    always;
    add_header Referrer-Policy         no-referrer-when-downgrade always;

    location ~* \.(?:js|css|png|jpe?g|gif|svg|woff2?)$ {
        root           /var/jenkins/workspace/evibes-frontend-nuxt/dist;
        try_files      $uri =404;
        add_header     Cache-Control "public, max-age=31536000, immutable";
        access_log     off;
    }

    location /favicon.ico {
        add_header Access-Control-Allow-Origin "*";
        root /var/jenkins/workspace/evibes/static;
    }

    location / {
        root           /var/jenkins/workspace/evibes-frontend-nuxt/dist;
        try_files      $uri $uri/ /index.html @maintenance;
    }

    location = /index.html {
        root           /var/jenkins/workspace/evibes-frontend-nuxt/dist;
        try_files      $uri @maintenance;
    }

    location = /robots.txt {
        add_header Access-Control-Allow-Origin "*";
        alias          /var/jenkins/workspace/evibes/static/robots_frontend.txt;
        default_type   text/plain;
    }

    location ~ ^/sitemap(?:-(?P<section>[a-z]+)(?:-(?P<page>\d+))?)?\.xml$ {
        proxy_pass         http://django_backend$request_uri;
        proxy_set_header   Host              $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;
        proxy_set_header   Accept            "application/xml";
    }

    location @maintenance {
        return 503;
    }

    error_page 503 404 500 502 504 /maintenance.html;
    location = /maintenance.html {
        add_header Access-Control-Allow-Origin "*";
        root           /var/jenkins/workspace/evibes/static;
        internal;
    }
}

server {
    listen 443 ssl http2;
    server_name prometheus.evibes.com;
    ssl_certificate     /etc/letsencrypt/live/evibes.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/evibes.com/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;
    ssl_session_cache   shared:SSL:10m;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header X-Frame-Options         SAMEORIGIN                 always;
    add_header X-Content-Type-Options  nosniff                    always;
    add_header Referrer-Policy         no-referrer-when-downgrade always;
    client_max_body_size 100M;

    location / {
        proxy_pass         http://prom_backend;
        proxy_set_header   Host               $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 = /robots.txt {
        add_header Access-Control-Allow-Origin "*";
        alias          /var/jenkins/workspace/evibes/static/robots_backend.txt;
        default_type   text/plain;
    }

    location /favicon.ico {
        add_header Access-Control-Allow-Origin "*";
        root /var/jenkins/workspace/evibes/static;
    }

    error_page 500 502 503 504 /maintenance.html;
    location = /maintenance.html {
        add_header Access-Control-Allow-Origin "*";
        root           /var/jenkins/workspace/evibes/static;
        internal;
    }
}

server {
    listen 80;
    server_name api.evibes.com b2b.evibes.com www.evibes.com evibes.com prometheus.evibes.com;
    return 301 https://$host$request_uri;
}
