/
githubmirror
/
LeetCode-Go
Обзор
Документация
Войти
/
githubmirror
/
LeetCode-Go
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
website/deploy/nginx-gitalk-oauth.conf
73 строки
4 KB
halfrost
update proxy
03 июл 2026, 02:27
03 июл 2026, 02:27
3bcc916
Код
Авторство
О чём код?
# Gitalk OAuth 反向代理 —— 修复点击「使用 GitHub 登录」报 # Error: Request failed with status code 502 / 504 # # 背景: # GitHub 的 token 接口 https://github.com/login/oauth/access_token 不返回 CORS 头, # 浏览器无法直接调用,必须由站点同源转发(config.toml 里 Gitalk.proxy = # https://books.halfrost.com/gitalk-oauth 就是指这里)。 # # 关于 502/504 的定位(重要): # GitHub 端一切正常(直接 POST 返回 200 的 incorrect_client_credentials JSON)。 # halfrost 的原配置里 proxy_pass 路径 / Host / SNI 都是对的,但仍间歇 502/504, # 根因在【上游连接健壮性】,按概率: # (1) DNS 过期(最可能):proxy_pass 用字面量 github.com 时,nginx 只在【启动时】解析一次 IP # 并一直沿用。GitHub 走 Fastly,IP 会轮换;启动时那个 IP 失效后 → 间歇 502/504。 # => 用 resolver + 变量改成【请求时实时解析】。 # (2) IPv6 陷阱:实时解析若返回 AAAA 而机器无 IPv6 出网 → 连接失败 502。=> resolver 加 ipv6=off。 # (3) HTTP 版本:默认 proxy_http_version 1.0,升 1.1 + Connection "" 更稳。 # 下面这版把以上三点都处理了。若换上后仍 502/504,看文件末尾「仍然 502 怎么办」拿 error.log 定位。 # # 用法:把 books.halfrost.com server{} 里现有的 location = /gitalk-oauth 整段替换为下面这段,然后 # nginx -t && nginx -s reload # 验证: # curl -s -o /dev/null -w '%{http_code}\n' -X POST https://books.halfrost.com/gitalk-oauth \ # -H 'Accept: application/json' \ # -d 'client_id=x&client_secret=x&code=x' # 修好后应返回 200(GitHub 回 incorrect_client_credentials 的 JSON,但 HTTP 状态是 200),不再是 502/504。 location = /gitalk-oauth { # 请求时实时解析 github.com,避免 nginx 启动时缓存的 IP 轮换失效导致间歇 502/504; # ipv6=off 防止返回 AAAA 而机器无 IPv6 出网导致连接失败。 resolver 1.1.1.1 8.8.8.8 valid=60s ipv6=off; set $gh_oauth https://github.com/login/oauth/access_token; proxy_pass $gh_oauth; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host github.com; # GitHub 按 Host 路由 proxy_ssl_server_name on; # 发送 SNI proxy_ssl_name github.com; proxy_ssl_protocols TLSv1.2 TLSv1.3; proxy_set_header Accept application/json; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 10s; proxy_send_timeout 10s; proxy_read_timeout 10s; } # ───────────────────────── 仍然 502/504 怎么办 ───────────────────────── # 看真正的报错(关键,一句话就能定性): # tail -n 50 /var/log/nginx/error.log # · "upstream timed out" → 服务器到 github 出网慢/被拦(见下方 Cloudflare 兜底) # · "no live upstreams" / "could not be resolved" → DNS 问题(确认 resolver 生效) # · "SSL_do_handshake() failed" → TLS/SNI 问题(确认 proxy_ssl_server_name on) # 再在【服务器上】直接测到 GitHub 的连通性: # curl -v -m 15 -X POST https://github.com/login/oauth/access_token \ # -H 'Accept: application/json' -d 'client_id=x&client_secret=x&code=x' # · 若服务器上这条 curl 也连不上/超时/reset → 是服务器出网到 github 的问题, # nginx 配置再怎么改都没用,改用下面的 Cloudflare Worker 兜底(部署在 GitHub 可达的边缘)。 # # ── 兜底方案:Cloudflare Worker(绕开本服务器出网,免费)── # 1. 在 Cloudflare 新建一个 Worker,代码: # export default { # async fetch(req) { # const url = 'https://github.com/login/oauth/access_token'; # const r = await fetch(url, { method: 'POST', headers: req.headers, body: req.body }); # const res = new Response(r.body, r); # res.headers.set('Access-Control-Allow-Origin', '*'); # return res; # } # } # 2. 把 config.toml 里的 Gitalk.proxy 改成该 Worker 的地址,重新构建发布即可。