From 3d3abeb65bf40bc9c6455375b8f3bde7904981c3 Mon Sep 17 00:00:00 2001 From: Alireza Hassani Date: Wed, 26 Aug 2026 19:10:15 +0330 Subject: [PATCH] Patch sitemap SEO proxies on both HTTP and HTTPS nginx blocks. Co-authored-by: Cursor --- scripts/websites-agent/patch-nginx-seo.sh | 131 ++++++++++++++-------- 1 file changed, 85 insertions(+), 46 deletions(-) diff --git a/scripts/websites-agent/patch-nginx-seo.sh b/scripts/websites-agent/patch-nginx-seo.sh index f42a735..9893956 100755 --- a/scripts/websites-agent/patch-nginx-seo.sh +++ b/scripts/websites-agent/patch-nginx-seo.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Insert / update Meshkee SEO proxy locations on an existing storefront nginx site. -# Covers: /sitemap.xml (index), child sitemaps, /robots.txt +# Covers HTTP + HTTPS server blocks: sitemap index, child sitemaps, robots.txt set -euo pipefail HOST="${1:-}" @@ -18,55 +18,94 @@ if [[ ! -f "$NGINX_AVAILABLE" ]]; then exit 1 fi -proxy_location() { - local path="$1" - local upstream="$2" - cat <"$tmp" - mv "$tmp" "$NGINX_AVAILABLE" - echo "added: ${path}" -} +def block(loc: str, upstream: str) -> str: + return ( + f" location = {loc} {{\n" + f" proxy_pass https://{api}/api/v1/tenants/{host}/{upstream};\n" + f" proxy_set_header Host {api};\n" + f" proxy_ssl_server_name on;\n" + f" proxy_set_header X-Real-IP $remote_addr;\n" + f" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" + f" proxy_set_header X-Forwarded-Proto $scheme;\n" + f" add_header Cache-Control \"public, max-age=900\";\n" + f" }}\n\n" + ) -ensure_location /sitemap.xml sitemap.xml -ensure_location /sitemap-main.xml sitemap-main.xml -ensure_location /sitemap-products.xml sitemap-products.xml -ensure_location /sitemap-blogs.xml sitemap-blogs.xml -ensure_location /sitemap-portfolios.xml sitemap-portfolios.xml -ensure_location /sitemap-videos.xml sitemap-videos.xml -ensure_location /sitemap-instructions.xml sitemap-instructions.xml -ensure_location /sitemap-workshops.xml sitemap-workshops.xml -ensure_location /robots.txt robots.txt +parts: list[tuple[str, str]] = [] +idx = 0 +while True: + start = text.find("server {", idx) + if start < 0: + parts.append(("tail", text[idx:])) + break + if start > idx: + parts.append(("other", text[idx:start])) + i = start + len("server {") + depth = 1 + while i < len(text) and depth: + if text[i] == "{": + depth += 1 + elif text[i] == "}": + depth -= 1 + i += 1 + parts.append(("server", text[start:i])) + idx = i + +changed = False +out: list[str] = [] +for kind, chunk in parts: + if kind != "server": + out.append(chunk) + continue + + server = chunk + if host not in server: + out.append(server) + continue + + insert_at = server.find(" location / {") + if insert_at < 0: + insert_at = server.find("\tlocation / {") + if insert_at < 0: + out.append(server) + continue + + additions = "" + for loc, upstream in paths: + if f"location = {loc}" not in server: + additions += block(loc, upstream) + changed = True + if additions: + server = server[:insert_at] + additions + server[insert_at:] + out.append(server) + +if changed: + path.write_text("".join(out)) + print(f"updated: {host}") +else: + print(f"already present: {host}") +PY nginx -t systemctl reload nginx