修改了代理

This commit is contained in:
史悦
2026-01-14 18:09:16 +08:00
parent 99d71f05cf
commit 54d97beb15
2 changed files with 45 additions and 53 deletions

View File

@@ -140,7 +140,7 @@
// Use relative path for API proxy to avoid CORS issues // Use relative path for API proxy to avoid CORS issues
// Nginx forwards /api/ to sync-server, which proxies /music-api to music-dl.sayqz.com // Nginx forwards /api/ to sync-server, which proxies /music-api to music-dl.sayqz.com
const API_BASE = "/music-api"; const API_BASE = "/api/music-api";
// Use relative path for sync service, assuming Nginx proxy is configured to forward /api/kv to the sync service // Use relative path for sync service, assuming Nginx proxy is configured to forward /api/kv to the sync service
const SYNC_API_BASE = "/api"; const SYNC_API_BASE = "/api";
@@ -270,15 +270,8 @@
return { results: [], total: 0 }; return { results: [], total: 0 };
} }
}, },
getSongUrl:async (id, source, br = '320k') => { getSongUrl: (id, source, br = '320k') => {
try { return `${API_BASE}?source=${source}&id=${id}&type=url&br=${br}`;
const res = await fetch(`${API_BASE}?source=${source}&id=${id}&type=url&br=${br}`);
const data = await res.json();
return data.url;
} catch (e) {
console.error("Get song url failed", e);
return null;
}
}, },
getPicUrl: (id, source) => { getPicUrl: (id, source) => {
return `${API_BASE}?source=${source}&id=${id}&type=pic`; return `${API_BASE}?source=${source}&id=${id}&type=pic`;
@@ -1277,19 +1270,11 @@
}, [playlist, currentSong, mode, volume, quality]); }, [playlist, currentSong, mode, volume, quality]);
useEffect(() => { useEffect(() => {
let isMounted = true;
if (currentSong) { if (currentSong) {
const audio = audioRef.current; const audio = audioRef.current;
if (!audio) return; if (!audio) return;
const fetchAndPlay = async () => {
// Update URL when quality changes or song changes // Update URL when quality changes or song changes
const url = await api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality); const url = api.getSongUrl(currentSong.id, currentSong.platform || currentSong.source, quality);
if (!isMounted || !url) return;
// Check if song changed while fetching
if (currentSongRef.current?.id !== currentSong.id) return;
// Only update src if it's different to avoid reloading same song on re-render (unless quality changed) // Only update src if it's different to avoid reloading same song on re-render (unless quality changed)
// Note: audioRef.current.src returns full absolute URL // Note: audioRef.current.src returns full absolute URL
@@ -1307,13 +1292,9 @@
} else if (deferOnIOS && wasPlaying) { } else if (deferOnIOS && wasPlaying) {
playAudioWithFallback(audio, { deferOnIOS: true }); playAudioWithFallback(audio, { deferOnIOS: true });
} }
};
fetchAndPlay();
} else { } else {
autoNextPendingRef.current = false; autoNextPendingRef.current = false;
} }
return () => { isMounted = false; };
}, [currentSong, quality]); // Re-run when quality changes }, [currentSong, quality]); // Re-run when quality changes
useEffect(() => { useEffect(() => {
@@ -1429,17 +1410,12 @@
if (options.immediate) { if (options.immediate) {
const audio = audioRef.current; const audio = audioRef.current;
if (!audio || !nextSong) return; if (!audio || !nextSong) return;
api.getSongUrl(nextSong.id, nextSong.platform || nextSong.source, quality).then(url => { const url = api.getSongUrl(nextSong.id, nextSong.platform || nextSong.source, quality);
if (!url) return;
// Ensure we are still trying to play the same song
if (currentSongRef.current?.id === nextSong.id) {
if (audio.src !== url) { if (audio.src !== url) {
audio.src = url; audio.src = url;
} }
playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS }); playAudioWithFallback(audio, { deferOnIOS: options.deferOnIOS });
} }
});
}
}; };
const playPrev = () => { const playPrev = () => {

View File

@@ -10,16 +10,32 @@ server {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} }
# 音乐 API 代理 (解决 HTTPS 握手和 Host 问题)
location /music-api {
# 将 /music-api 映射到 /api
# 例如: /music-api?source=... -> https://music-dl.sayqz.com/api?source=...
rewrite ^/music-api/?(.*)$ /api/$1 break;
proxy_pass https://music-dl.sayqz.com;
# [关键] 开启 SSL Server Name Indication (SNI),否则 HTTPS 握手会失败
proxy_ssl_server_name on;
# [关键] 设置正确的 Host 头,或者直接删掉这行让 Nginx 自动使用 proxy_pass 的域名
# 千万不要设置为 $host因为对方服务器不认识你的域名
proxy_set_header Host music-dl.sayqz.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 同步服务 API 代理 # 同步服务 API 代理
# 将 /api/kv/... 转发到 sync-service 的 7482 端口
# 注意:这里去掉了 /api 前缀,因为 sync-service 直接监听 /kv
location /api/ { location /api/ {
proxy_pass http://127.0.0.1:7482/; proxy_pass http://127.0.0.1:7482/;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 允许跨域 (如果前端和后端不在同一个域)
add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';