Patch sitemap SEO proxies on both HTTP and HTTPS nginx blocks.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
730407ec3a
commit
3d3abeb65b
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Insert / update Meshkee SEO proxy locations on an existing storefront nginx site.
|
# 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
|
set -euo pipefail
|
||||||
|
|
||||||
HOST="${1:-}"
|
HOST="${1:-}"
|
||||||
@@ -18,55 +18,94 @@ if [[ ! -f "$NGINX_AVAILABLE" ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
proxy_location() {
|
python3 - "$NGINX_AVAILABLE" "$HOST" "$API_HOST" <<'PY'
|
||||||
local path="$1"
|
import sys
|
||||||
local upstream="$2"
|
from pathlib import Path
|
||||||
cat <<NGINX
|
|
||||||
location = ${path} {
|
|
||||||
proxy_pass https://${API_HOST}/api/v1/tenants/${HOST}/${upstream};
|
|
||||||
proxy_set_header Host ${API_HOST};
|
|
||||||
proxy_ssl_server_name on;
|
|
||||||
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;
|
|
||||||
add_header Cache-Control "public, max-age=900";
|
|
||||||
}
|
|
||||||
|
|
||||||
NGINX
|
path = Path(sys.argv[1])
|
||||||
}
|
host = sys.argv[2]
|
||||||
|
api = sys.argv[3]
|
||||||
|
text = path.read_text()
|
||||||
|
|
||||||
ensure_location() {
|
paths = [
|
||||||
local path="$1"
|
("/sitemap.xml", "sitemap.xml"),
|
||||||
local upstream="$2"
|
("/sitemap-main.xml", "sitemap-main.xml"),
|
||||||
if grep -q "location = ${path}" "$NGINX_AVAILABLE"; then
|
("/sitemap-products.xml", "sitemap-products.xml"),
|
||||||
echo "already present: ${path}"
|
("/sitemap-blogs.xml", "sitemap-blogs.xml"),
|
||||||
return 0
|
("/sitemap-portfolios.xml", "sitemap-portfolios.xml"),
|
||||||
fi
|
("/sitemap-videos.xml", "sitemap-videos.xml"),
|
||||||
|
("/sitemap-instructions.xml", "sitemap-instructions.xml"),
|
||||||
|
("/sitemap-workshops.xml", "sitemap-workshops.xml"),
|
||||||
|
("/robots.txt", "robots.txt"),
|
||||||
|
]
|
||||||
|
|
||||||
local block
|
def block(loc: str, upstream: str) -> str:
|
||||||
block="$(proxy_location "$path" "$upstream")"
|
return (
|
||||||
local tmp
|
f" location = {loc} {{\n"
|
||||||
tmp="$(mktemp)"
|
f" proxy_pass https://{api}/api/v1/tenants/{host}/{upstream};\n"
|
||||||
awk -v block="$block" '
|
f" proxy_set_header Host {api};\n"
|
||||||
/location \/ \{/ && !done {
|
f" proxy_ssl_server_name on;\n"
|
||||||
print block
|
f" proxy_set_header X-Real-IP $remote_addr;\n"
|
||||||
done = 1
|
f" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"
|
||||||
}
|
f" proxy_set_header X-Forwarded-Proto $scheme;\n"
|
||||||
{ print }
|
f" add_header Cache-Control \"public, max-age=900\";\n"
|
||||||
' "$NGINX_AVAILABLE" >"$tmp"
|
f" }}\n\n"
|
||||||
mv "$tmp" "$NGINX_AVAILABLE"
|
)
|
||||||
echo "added: ${path}"
|
|
||||||
}
|
|
||||||
|
|
||||||
ensure_location /sitemap.xml sitemap.xml
|
parts: list[tuple[str, str]] = []
|
||||||
ensure_location /sitemap-main.xml sitemap-main.xml
|
idx = 0
|
||||||
ensure_location /sitemap-products.xml sitemap-products.xml
|
while True:
|
||||||
ensure_location /sitemap-blogs.xml sitemap-blogs.xml
|
start = text.find("server {", idx)
|
||||||
ensure_location /sitemap-portfolios.xml sitemap-portfolios.xml
|
if start < 0:
|
||||||
ensure_location /sitemap-videos.xml sitemap-videos.xml
|
parts.append(("tail", text[idx:]))
|
||||||
ensure_location /sitemap-instructions.xml sitemap-instructions.xml
|
break
|
||||||
ensure_location /sitemap-workshops.xml sitemap-workshops.xml
|
if start > idx:
|
||||||
ensure_location /robots.txt robots.txt
|
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
|
nginx -t
|
||||||
systemctl reload nginx
|
systemctl reload nginx
|
||||||
|
|||||||
Reference in New Issue
Block a user