From 225c3a5ded664d71626a0a51312434c2f149000a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=BB=E5=8B=87=E7=A5=A5?= <237899745@qq.com> Date: Fri, 21 Nov 2025 16:54:26 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20=E6=9B=B4=E6=96=B0install.sh=20-?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0Nginx=20Cookie=E4=BC=A0=E9=80=92=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 修改说明 install.sh生成的Nginx配置中缺少Cookie传递设置, 导致验证码session无法正常工作。 ## 修改内容 ### install.sh 在3处 `location /api` 配置中添加Cookie传递: ```nginx # Cookie传递配置(验证码session需要) proxy_set_header Cookie $http_cookie; proxy_pass_header Set-Cookie; ``` 修改位置: - 行2383: HTTP配置(configure_nginx_http_first) - 行2665: HTTPS配置(configure_nginx_final) - 行2802: 虚拟主机配置 ### 新增文件 1. **fix_install_sh.sh** - 自动化修改脚本 - 备份原文件后自动添加Cookie配置 - 可重复运行(检测已修改则跳过) 2. **INSTALL_SH_UPDATE.md** - 详细的修改说明文档 - 手动修改方法 - 验证和应用指南 ## 使用方法 ### 新部署 直接运行修改后的install.sh即可 ### 自动修改 ```bash ./fix_install_sh.sh ``` ### 已部署服务 需要手动更新Nginx配置: ```bash vim /etc/nginx/conf.d/玩玩云.conf # 在 location /api 块中添加Cookie配置 nginx -t nginx -s reload ``` ## 配套修复 此修改配合以下提交才能完整工作: - 后端session配置 (7ce9d95) - 前端axios配置 (83773ef) - nginx/nginx.conf (5f3fd38) - install.sh (本提交) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- INSTALL_SH_UPDATE.md | 192 +++++++++++++++++++++++++++++++++++++++++++ fix_install_sh.sh | 82 ++++++++++++++++++ install.sh | 12 +++ 3 files changed, 286 insertions(+) create mode 100644 INSTALL_SH_UPDATE.md create mode 100755 fix_install_sh.sh diff --git a/INSTALL_SH_UPDATE.md b/INSTALL_SH_UPDATE.md new file mode 100644 index 0000000..4d64eeb --- /dev/null +++ b/INSTALL_SH_UPDATE.md @@ -0,0 +1,192 @@ +# install.sh 更新说明 - 验证码Cookie配置 + +## 需要修改的原因 + +验证码功能依赖Cookie传递session信息。install.sh生成的Nginx配置中缺少Cookie传递设置,导致验证码无法正常工作。 + +## 需要修改的位置 + +install.sh文件中有**3处**`location /api`配置需要添加Cookie传递设置。 + +### 位置1:第2372-2387行(HTTP配置) + +在 `configure_nginx_http_first()` 函数中: + +```nginx +location /api { + proxy_pass http://localhost:${BACKEND_PORT}; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + 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; + + # ⬇️ 在这里添加以下3行 ⬇️ + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + + # 上传超时设置 + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_connect_timeout 300s; +} +``` + +### 位置2:第2650行附近(HTTPS配置) + +在 `configure_nginx_final()` 函数的HTTPS配置中: + +```nginx +location /api { + proxy_pass http://localhost:${BACKEND_PORT}; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + 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; + + # ⬇️ 在这里添加以下3行 ⬇️ + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + + # 上传超时设置 + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_connect_timeout 300s; +} +``` + +### 位置3:第2783行附近(虚拟主机配置) + +在另一个Nginx配置块中: + +```nginx +location /api { + proxy_pass http://localhost:${BACKEND_PORT}; + # ... 其他配置 ... + proxy_set_header X-Forwarded-Proto $scheme; + + # ⬇️ 在这里添加以下3行 ⬇️ + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + + # 上传超时设置 + # ... +} +``` + +## 手动修改方法 + +### 方法1:使用vim编辑器 + +```bash +vim install.sh + +# 在vim中: +# 1. 按 / 搜索:location /api +# 2. 找到 proxy_set_header X-Forwarded-Proto $scheme; 这一行 +# 3. 在其后添加3行Cookie配置 +# 4. 按 n 继续查找下一个,重复步骤2-3 +# 5. 共修改3处 +# 6. :wq 保存退出 +``` + +### 方法2:使用sed批量替换 + +```bash +cd /home/yuyx/aaaaaa/网盘/vue-driven-cloud-storage + +# 备份 +cp install.sh install.sh.backup + +# 批量替换(在所有location /api块中的X-Forwarded-Proto后添加) +# 注意:这个命令需要仔细测试 +sed -i '/proxy_set_header X-Forwarded-Proto \\$scheme;/a\ +\ + # Cookie传递配置(验证码session需要)\ + proxy_set_header Cookie $http_cookie;\ + proxy_pass_header Set-Cookie;' install.sh + +# 验证修改 +grep -A 2 "Cookie传递配置" install.sh +``` + +### 方法3:手动复制粘贴 + +1. 打开install.sh +2. 搜索 `location /api`(会找到3处) +3. 在每处的 `proxy_set_header X-Forwarded-Proto $scheme;` 后面添加: + +``` + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; +``` + +## 验证修改 + +```bash +# 检查是否修改了3处 +grep -c "Cookie传递配置" install.sh +# 输出应该是: 3 + +# 查看修改的位置 +grep -n "Cookie传递配置" install.sh +``` + +## 应用修改 + +### 新部署时 +直接运行修改后的install.sh即可,它会生成包含Cookie配置的Nginx配置文件。 + +### 已部署的服务 +需要手动更新Nginx配置文件: + +```bash +# 编辑Nginx配置 +vim /etc/nginx/sites-available/玩玩云.conf +# 或 +vim /etc/nginx/conf.d/玩玩云.conf + +# 在location /api块中添加Cookie配置 +# 测试配置 +nginx -t + +# 重新加载 +nginx -s reload +``` + +## 为什么需要这个配置 + +1. **proxy_set_header Cookie $http_cookie;** + - 将浏览器发送的Cookie转发给后端 + - 后端才能读取验证码session + +2. **proxy_pass_header Set-Cookie;** + - 将后端的Set-Cookie响应头传递给浏览器 + - 浏览器才能保存验证码session cookie + +3. **缺少这两行会导致**: + - 验证码session无法建立 + - 验证码一直提示"已过期" + - Cookie在Nginx层被过滤掉 + +## 相关提交 + +- nginx/nginx.conf 已经修改(提交 5f3fd38) +- install.sh 需要同步更新(本文档) + +## 注意事项 + +- 修改install.sh后,需要提交到Git +- 已部署的服务需要手动更新Nginx配置 +- 修改后记得测试验证码功能 diff --git a/fix_install_sh.sh b/fix_install_sh.sh new file mode 100755 index 0000000..0fa2310 --- /dev/null +++ b/fix_install_sh.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# 自动修改install.sh添加Cookie配置的脚本 + +set -e + +echo "=========================================" +echo "install.sh Cookie配置自动修改脚本" +echo "=========================================" +echo "" + +# 检查文件是否存在 +if [[ ! -f "install.sh" ]]; then + echo "❌ 错误: install.sh文件不存在" + exit 1 +fi + +# 备份 +echo "📦 备份install.sh..." +cp install.sh install.sh.backup.$(date +%Y%m%d%H%M%S) +echo "✅ 备份完成" +echo "" + +# 检查是否已经修改过 +if grep -q "Cookie传递配置" install.sh; then + echo "ℹ️ 检测到install.sh已经包含Cookie配置" + echo " 跳过修改" + exit 0 +fi + +echo "🔧 开始修改install.sh..." +echo "" + +# 使用perl进行多行匹配和替换 +perl -i -pe ' +BEGIN { + $cookie_config = "\n" . + " # Cookie传递配置(验证码session需要)\n" . + " proxy_set_header Cookie \$http_cookie;\n" . + " proxy_pass_header Set-Cookie;"; +} + +# 在 location /api 块中的 X-Forwarded-Proto 后添加Cookie配置 +if (/proxy_set_header X-Forwarded-Proto \\\$scheme;/ && $in_api_block) { + $_ .= $cookie_config . "\n"; + $added_count++; +} + +$in_api_block = 1 if /location \/api/; +$in_api_block = 0 if /^\s*\}/; +' install.sh + +# 验证修改结果 +count=$(grep -c "Cookie传递配置" install.sh || echo "0") + +echo "=========================================" +echo "修改完成" +echo "=========================================" +echo "" +echo "📊 修改统计:" +echo " - 添加Cookie配置: $count 处" +echo "" + +if [[ "$count" -eq 3 ]]; then + echo "✅ 成功!修改了3处location /api配置" + echo "" + echo "修改位置:" + grep -n "Cookie传递配置" install.sh | sed 's/^/ 行 /' + echo "" + echo "📝 建议:" + echo " 1. 检查修改是否正确: diff install.sh.backup.* install.sh" + echo " 2. 提交到Git: git add install.sh && git commit -m '添加Cookie配置'" + echo " 3. 已部署的服务需要手动更新Nginx配置" +elif [[ "$count" -gt 0 ]]; then + echo "⚠️ 警告: 只修改了 $count 处,预期应该是3处" + echo " 请手动检查install.sh文件" +else + echo "❌ 错误: 修改失败,没有添加任何Cookie配置" + echo " 请查看 INSTALL_SH_UPDATE.md 手动修改" +fi + +echo "" diff --git a/install.sh b/install.sh index ab243e4..01b8f2e 100644 --- a/install.sh +++ b/install.sh @@ -2380,6 +2380,10 @@ server { proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + # 上传超时设置 proxy_read_timeout 3600s; proxy_send_timeout 3600s; @@ -2658,6 +2662,10 @@ server { proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + # 上传超时设置(大文件上传需要更长时间,设置为1小时) proxy_read_timeout 3600s; proxy_send_timeout 3600s; @@ -2791,6 +2799,10 @@ server { proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; + # Cookie传递配置(验证码session需要) + proxy_set_header Cookie $http_cookie; + proxy_pass_header Set-Cookie; + # 上传超时设置(大文件上传需要更长时间,设置为1小时) proxy_read_timeout 3600s; proxy_send_timeout 3600s;