worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-back {
        server back:8000;
    }

    types {
        module js;
    }
    include       /etc/nginx/mime.types;

    server {
        listen 80;

        gzip on;
        gzip_http_version 1.1;
        gzip_disable      "MSIE [1-6]\.";
        gzip_min_length   256;
        gzip_vary         on;
        gzip_proxied      expired no-cache no-store private auth;
        gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_comp_level   9;

        root /usr/share/nginx/html;

        location / {
            try_files $uri $uri/ /index.html?$args;
        }

        location ~* ^.+\.css$ {
            default_type text/css;
        }

        location ~* ^.+\.js$ {
            default_type text/javascript;
        }

        location /contracts/signature/ {
            set $is_robot 0;
            if ($http_user_agent ~* "Discordbot|googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp") {
                rewrite /contracts/signature/(.*) /api/v1/contract/print/opengraph/$1 last;
                proxy_pass         http://docker-back;
                set $is_robot 1;
            }

            if ($is_robot = 0) {
                rewrite ^ /index.html?$args last;
            }

            proxy_redirect     off;
            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-Host $server_name;
        }

        location /api/v1/ {
            proxy_pass         http://docker-back/;
            proxy_redirect     off;
            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-Host $server_name;
        }
    }
}
