## 修改说明 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 <noreply@anthropic.com>
4.8 KiB
4.8 KiB
install.sh 更新说明 - 验证码Cookie配置
需要修改的原因
验证码功能依赖Cookie传递session信息。install.sh生成的Nginx配置中缺少Cookie传递设置,导致验证码无法正常工作。
需要修改的位置
install.sh文件中有3处location /api配置需要添加Cookie传递设置。
位置1:第2372-2387行(HTTP配置)
在 configure_nginx_http_first() 函数中:
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配置中:
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配置块中:
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编辑器
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批量替换
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:手动复制粘贴
- 打开install.sh
- 搜索
location /api(会找到3处) - 在每处的
proxy_set_header X-Forwarded-Proto $scheme;后面添加:
# Cookie传递配置(验证码session需要)
proxy_set_header Cookie $http_cookie;
proxy_pass_header Set-Cookie;
验证修改
# 检查是否修改了3处
grep -c "Cookie传递配置" install.sh
# 输出应该是: 3
# 查看修改的位置
grep -n "Cookie传递配置" install.sh
应用修改
新部署时
直接运行修改后的install.sh即可,它会生成包含Cookie配置的Nginx配置文件。
已部署的服务
需要手动更新Nginx配置文件:
# 编辑Nginx配置
vim /etc/nginx/sites-available/玩玩云.conf
# 或
vim /etc/nginx/conf.d/玩玩云.conf
# 在location /api块中添加Cookie配置
# 测试配置
nginx -t
# 重新加载
nginx -s reload
为什么需要这个配置
-
proxy_set_header Cookie $http_cookie;
- 将浏览器发送的Cookie转发给后端
- 后端才能读取验证码session
-
proxy_pass_header Set-Cookie;
- 将后端的Set-Cookie响应头传递给浏览器
- 浏览器才能保存验证码session cookie
-
缺少这两行会导致:
- 验证码session无法建立
- 验证码一直提示"已过期"
- Cookie在Nginx层被过滤掉
相关提交
- nginx/nginx.conf 已经修改(提交 5f3fd38)
- install.sh 需要同步更新(本文档)
注意事项
- 修改install.sh后,需要提交到Git
- 已部署的服务需要手动更新Nginx配置
- 修改后记得测试验证码功能