Skip to content

Instantly share code, notes, and snippets.

@khanha2
Last active November 4, 2025 14:44
Show Gist options
  • Select an option

  • Save khanha2/8863657d1da955f74b00a1ae2f4015d0 to your computer and use it in GitHub Desktop.

Select an option

Save khanha2/8863657d1da955f74b00a1ae2f4015d0 to your computer and use it in GitHub Desktop.
TSMT 2025 proxy
server {
    listen 443 ssl;
    server_name tawk.smiv.vn;

    ssl_certificate /etc/letsencrypt/live/tawk.smiv.vn/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/tawk.smiv.vn/privkey.pem; # managed by Certbot

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    resolver 8.8.8.8 valid=10s;

    location /do/va/ {	
        proxy_pass https://va.tawk.to/;	
        proxy_set_header Host va.tawk.to;	
        proxy_ssl_server_name on;	


        set $subdomain $1;
        set $rest $2;

        if ($rest = "") {
            set $rest "/";
        }


        proxy_http_version 1.1;

        proxy_set_header Accept-Encoding "";    # Disable compression for sub_filter to work
        proxy_buffering off;

        header_filter_by_lua_block {
            -- Enable response body modification
            ngx.header.content_length = nil
        }

        body_filter_by_lua_block {
            local chunk = ngx.arg[1]
            local eof = ngx.arg[2]

            if not ngx.ctx.buffer then
                ngx.ctx.buffer = {}
            end

            if chunk then
                table.insert(ngx.ctx.buffer, chunk)
            end

            if eof then
                local whole = table.concat(ngx.ctx.buffer)
                whole = whole:gsub("https://va%.tawk%.to", "https://tawk.smiv.vn/do/va")
                whole = whole:gsub("https://([%w%-_]+)%.tawk%.to", "https://tawk.smiv.vn/domain/%1")
                whole = whole:gsub('vss"%s*:%s*"([%w%-_]+)%.tawk%.to', 'vss":"tawk.smiv.vn/domain/%1')
                ngx.arg[1] = whole
            else
                ngx.arg[1] = nil  -- Ignore intermediate chunks
            end
        }

        sub_filter https://tawk.to https://tawk.smiv.vn;

        sub_filter_types text/html text/javascript application/javascript application/x-javascript application/json text/css;
    }	

    location ~ ^/domain/([^/]+)(/.*)?$ {
        set $subdomain $1;
        set $rest $2;

        if ($rest = "") {
            set $rest "/";
        }

        proxy_pass https://$subdomain.tawk.to$rest;

        proxy_http_version 1.1;

        proxy_set_header Host $subdomain.tawk.to;
        proxy_ssl_server_name on;
        proxy_set_header Accept-Encoding "";    # Disable compression for sub_filter to work
        proxy_buffering off;

        header_filter_by_lua_block {
            -- Enable response body modification
            ngx.header.content_length = nil
        }

        body_filter_by_lua_block {
            local chunk = ngx.arg[1]
            local eof = ngx.arg[2]

            if not ngx.ctx.buffer then
                ngx.ctx.buffer = {}
            end

            if chunk then
                table.insert(ngx.ctx.buffer, chunk)
            end

            if eof then
                local whole = table.concat(ngx.ctx.buffer)
                whole = whole:gsub("https://va%.tawk%.to", "https://tawk.smiv.vn/do/va")                
                whole = whole:gsub("https://([%w%-_]+)%.tawk%.to", "https://tawk.smiv.vn/domain/%1")
                ngx.arg[1] = whole
            else
                ngx.arg[1] = nil  -- Ignore intermediate chunks
            end
        }

        sub_filter https://tawk.to https://tawk.smiv.vn;

        sub_filter_types text/html text/javascript application/javascript application/x-javascript application/json text/css;
    }

    location / {
        proxy_pass https://tawk.to/;
        proxy_set_header Host tawk.to;
        proxy_ssl_server_name on;
        proxy_set_header Accept-Encoding "";    # Disable compression for sub_filter to work
        proxy_buffering off;

        header_filter_by_lua_block {
            -- Enable response body modification
            ngx.header.content_length = nil
        }

        body_filter_by_lua_block {
            local chunk = ngx.arg[1]
            local eof = ngx.arg[2]

            if not ngx.ctx.buffer then
                ngx.ctx.buffer = {}
            end

            if chunk then
                .insert(ngx.ctx.buffer, chunk)
            end

            if eof then
                local whole = table.concat(ngx.ctx.buffer)
                whole = whole:gsub("https://([%w%-_]+)%.tawk%.to", "https://tawk.smiv.vn/domain/%1")
                ngx.arg[1] = whole
            else
                ngx.arg[1] = nil  -- Ignore intermediate chunks
            end
        }

        sub_filter https://tawk.to https://tawk.smiv.vn;

        sub_filter_types text/html text/javascript application/javascript application/x-javascript application/json text/css;
    }
}

server {
    listen 80;
    server_name tawk.smiv.vn;

    return 301 https://$host$request_uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment