montana/Russian/Site/montana-archive/nginx/patch.sh
2026-05-18 18:05:32 +03:00

80 lines
3.0 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Идемпотентно дописывает в nginx-конфиг efir_org локации /archive/ и /llms.txt
# и в hub_montana_quest — /robots.txt + CORS.
set -euo pipefail
EFIR=/etc/nginx/sites-available/efir_org
HUB=/etc/nginx/sites-available/hub_montana_quest
# ── 1. /archive/ на montana.quest ──
if ! grep -q "location /archive/" "$EFIR"; then
python3 <<PY
import re, pathlib
p = pathlib.Path("$EFIR"); s = p.read_text()
# найти первый server-блок с listen 443 для montana.quest
m = re.search(r'(server\s*\{[^}]*server_name[^;]*montana\.quest[^}]*\{[^}]*\}|server\s*\{(?:[^{}]|\{[^{}]*\})*server_name[^;]*montana\.quest(?:[^{}]|\{[^{}]*\})*)\}', s, re.S)
if not m:
raise SystemExit("montana.quest server block not found in efir_org")
block = m.group(0)
# вставляем перед закрывающей }
patch = """
location = /llms.txt { root /var/www/montana_quest; add_header Access-Control-Allow-Origin "*" always; add_header Content-Type "text/plain; charset=utf-8" always; }
location ^~ /archive/ {
alias /var/www/montana_archive/;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
add_header Access-Control-Allow-Origin "*" always;
add_header X-Robots-Tag "all" always;
# большие файлы — отдаём sendfile'ом, без буфера в памяти
sendfile on; tcp_nopush on;
gzip off;
}
"""
new_block = block[:-1] + patch + "\n}"
s = s.replace(block, new_block, 1)
p.write_text(s)
print("efir_org: добавлен /archive/ + /llms.txt")
PY
else
echo "efir_org: /archive/ уже есть"
fi
# ── 2. /robots.txt + CORS на hub.montana.quest ──
if ! grep -q "location = /robots.txt" "$HUB"; then
python3 <<PY
import re, pathlib
p = pathlib.Path("$HUB"); s = p.read_text()
# вставляем перед "location / {" с proxy_pass на Gitea
m = re.search(r'(\n[ \t]*location / \{\n[ \t]*proxy_pass http://127\.0\.0\.1:3000;)', s)
if not m:
raise SystemExit("location / { proxy_pass... } in hub_montana_quest not found")
patch = """
location = /robots.txt {
root /var/www/montana_hub_static;
add_header Access-Control-Allow-Origin "*" always;
add_header Content-Type "text/plain; charset=utf-8" always;
}
location = /llms.txt {
root /var/www/montana_hub_static;
add_header Access-Control-Allow-Origin "*" always;
add_header Content-Type "text/plain; charset=utf-8" always;
}
"""
s = s.replace(m.group(1), patch + m.group(1), 1)
# CORS внутри proxy_pass на /
s = re.sub(
r'(location / \{\n[ \t]*proxy_pass http://127\.0\.0\.1:3000;)',
r'\1\n add_header Access-Control-Allow-Origin "*" always;\n add_header X-Robots-Tag "all" always;',
s, count=1
)
p.write_text(s)
print("hub_montana_quest: добавлен /robots.txt + /llms.txt + CORS")
PY
else
echo "hub_montana_quest: /robots.txt уже есть"
fi
nginx -t && systemctl reload nginx
echo "nginx перезагружен"