Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion website/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ defaultContentLanguageInSubdir = true
# 登录代理:GitHub 的 /login/oauth/access_token 接口不返回 CORS 头,浏览器无法直接调用,必须经服务端中转。
# 原来的 Heroku 免费应用已停服,故改为指向本站同源路径,由服务器反向代理到 GitHub。
# 因为与站点同源(都是 books.halfrost.com),浏览器不触发 CORS,最稳。
# 需要在服务器 web 配置里加一个反代到 https://github.com/login/oauth/access_token 的 location(见对话里的 nginx 片段)。
# 需要在服务器 web 配置里加一个反代到 https://github.com/login/oauth/access_token 的 location。
# 完整且修好 502(SNI) 的 nginx 配置见 website/deploy/nginx-gitalk-oauth.conf。
proxy= "https://books.halfrost.com/gitalk-oauth"

68 changes: 68 additions & 0 deletions website/deploy/nginx-gitalk-oauth.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Gitalk OAuth 反向代理 —— 修复点击「使用 GitHub 登录」报
# Error: Request failed with status code 502
#
# 背景:
# GitHub 的 token 接口 https://github.com/login/oauth/access_token 不返回 CORS 头,
# 浏览器无法直接调用,必须由站点同源转发(config.toml 里 Gitalk.proxy =
# https://books.halfrost.com/gitalk-oauth 就是指这里)。
#
# 关于 502 的定位(重要):
# 经实测,GitHub 端一切正常——不带 SNI 也能握手,直接 POST 返回 200(incorrect_client_credentials
# 的 JSON)。所以 502 是【本服务器侧】的故障,最常见两类:
# (1) 原来的 /gitalk-oauth 没有直连 GitHub,而是转发给一个本地中转程序
# (gatekeeper / cors-anywhere 之类的 node 应用),该进程挂了 → nginx 502。
# (2) 服务器到 github.com 出网被阻断/重置(国内主机常见)→ upstream 连接失败 → 502。
# 下面的配置改成【nginx 直连 GitHub】,可消除 (1)。若换上后仍 502,基本就是 (2),
# 请看文件末尾的「仍然 502 怎么办」。
#
# 用法:把下面的 location 放进 books.halfrost.com 的 server {} 块,然后
# 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。

location = /gitalk-oauth {
proxy_http_version 1.1;
proxy_pass https://github.com/login/oauth/access_token;

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 X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept application/json;

# 连接到 GitHub 慢/不稳时,给足超时,避免过早 502
proxy_connect_timeout 15s;
proxy_read_timeout 15s;

# 若改用变量形式的 proxy_pass(set $up https://github.com;)才需要显式 DNS:
# resolver 1.1.1.1 8.8.8.8 valid=300s;
}

# ───────────────────────── 仍然 502 怎么办 ─────────────────────────
# 先看真正的报错(关键):
# tail -n 50 /var/log/nginx/error.log
# 再在【服务器上】直接测到 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 的问题(上面的(2)),
# 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 的地址,重新构建发布即可。
Binary file added website/public/LeetCode_Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/LeetCode_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/LeetCode_logo2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-1024x1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-120x120.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-152x152.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-180x180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/apple-touch-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading
Loading