add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

upstream kibana {
    server localhost:5601;
}

upstream openviduserver {
    server localhost:5443;
}

server {
    # Redirect to https
    if ($host = {domain_name}) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 default_server;
    server_name {domain_name};

    # letsencrypt
    location /.well-known/acme-challenge {
        root /var/www/certbot;
        try_files $uri $uri/ =404;
    }

    # Kibana panel
    location /kibana {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        rewrite ^/kibana/(.*)$ /$1 break;
        proxy_pass http://kibana/;
    }
}

server {
    listen 443 ssl default deferred;
    server_name {domain_name};

    ssl_certificate         /etc/letsencrypt/live/{domain_name}/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/{domain_name}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/{domain_name}/fullchain.pem;
    
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 5m;
    ssl_stapling on;
    ssl_stapling_verify on;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";

    ssl_prefer_server_ciphers on;

    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 X-Forwarded-Proto https;
    proxy_headers_hash_bucket_size 512;
    proxy_redirect off;

    # Websockets
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    # Welcome
    root /var/www/html;

    # Kibana panel
    location /kibana {
        auth_basic "Openvidu Monitoring";
        auth_basic_user_file /etc/nginx/kibana.htpasswd;

        rewrite ^/kibana/(.*)$ /$1 break;
        proxy_pass http://kibana/;
    }

    # Openvidu Server
    location /layouts/custom {
        rewrite ^/layouts/custom/(.*)$ /custom-layout/$1 break;
        root /opt/openvidu;
    }

    location /api {
        proxy_pass http://openviduserver;
    }

    location /openvidu {
        proxy_pass http://openviduserver;
    }

    location /info {
        proxy_pass http://openviduserver;
    }

    location /config {
        proxy_pass http://openviduserver;
    }

    location /accept-certificate {
        proxy_pass http://openviduserver;
    }

    location /dashboard {
        rewrite ^/dashboard/(.*)$ /$1 break;
        proxy_pass http://openviduserver/;
    }

    # Openvidu Server Pro
    location /cdr {
        proxy_pass http://openviduserver;
    }

    location /pro {
        proxy_pass http://openviduserver;
    }

    location /api-login {
        proxy_pass http://openviduserver;
    }

    location /elasticsearch {
        proxy_pass http://openviduserver;
    }

    location /inspector {
        rewrite ^/inspector/(.*)$ /$1 break;
        proxy_pass http://openviduserver/;
    }
}
