Files
yuyueguahao/scripts/fix-nginx-mirror.sh
2026-07-05 14:43:34 +08:00

47 lines
1.8 KiB
Bash

#!/bin/bash
# 把 /uploads/ 的 proxy_pass 改成和 /api/ 完全一致, 加 proxy_http_version 等
cat > /tmp/new-uploads-loc.txt << 'NGINX'
# 静态资源上传目录(Express 落盘位置, 反代到本机 3000)
location /uploads/ {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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-Proto $scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
}
NGINX
# 替换 /uploads/ 那块
python3 -c "
import re
with open('/etc/nginx/sites-enabled/yuyueguahao', 'r') as f:
content = f.read()
with open('/tmp/new-uploads-loc.txt', 'r') as f:
new_loc = f.read().rstrip()
# 用 re 替换原 /uploads/ 整个 location 块
pattern = r' # 静态资源上传目录.*?\n \}\n'
new = re.sub(pattern, new_loc + '\n', content, flags=re.DOTALL)
with open('/etc/nginx/sites-enabled/yuyueguahao', 'w') as f:
f.write(new)
print('OK')
"
echo "--- Updated /uploads/ location ---"
grep -A12 'location /uploads/' /etc/nginx/sites-enabled/yuyueguahao
echo "--- nginx -t ---"
nginx -t 2>&1 | tail -3
echo "--- reload ---"
NGINX_MASTER=$(ps -efL | grep "nginx: master" | grep -v grep | awk '{print $2}' | head -1)
echo "Master PID: $NGINX_MASTER"
kill -HUP "$NGINX_MASTER"
sleep 2
echo "--- TEST 80 ---"
curl -o /dev/null -s -w "HTTP %{http_code} size=%{size_download} type=%{content_type}\n" http://localhost/uploads/1781403331659-1cac3cac.png
echo "--- TEST API sanity ---"
curl -o /dev/null -s -w "HTTP %{http_code}\n" http://localhost/api/v1/banners
echo "--- error log tail ---"
tail -3 /var/log/nginx/error.log