nginx反向代理未正确配置导致cors报错,需安装启动nginx、配置codex.conf中proxy_pass保留末尾斜杠、前置options预检拦截或map白名单,并将前端api请求改为相对路径/api/。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜

你在Linux服务器上部署了Codex前端应用,但调用后端API时浏览器控制台持续报错“CORS header ‘Access-Control-Allow-Origin’ missing”,请求被拦截,页面无法加载数据。这不是前端代码问题,而是Nginx未正确处理跨域请求或代理路径配置失效导致的真实链路中断。
确认Nginx已安装并运行
执行 nginx -v 查看版本,若提示 command not found,则需先安装:Ubuntu/Debian系统运行 sudo apt update && sudo apt install nginx -y;CentOS/RHEL系统运行 sudo yum install epel-release -y && sudo yum install nginx -y。
启动服务并设为开机自启:sudo systemctl start nginx && sudo systemctl enable nginx。用 curl -I http://localhost 检查返回 HTTP 200,说明Nginx基础服务已就绪。
配置反向代理转发API请求
进入站点配置目录:sudo nano /etc/nginx/conf.d/codex.conf(若不存在则新建)。
写入以下server块(注意:server_name必须与你实际访问Codex的域名或IP一致,如开发环境常用 【server_name localhost;】):
server {<br> listen 80;<br> server_name localhost;<br> root /var/www/codex;<br> index index.html;<br><br> location /api/ {<br> proxy_pass http://127.0.0.1:3001/;<br> proxy_set_header Host $host;<br> proxy_set_header X-Real-IP $remote_addr;<br> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br> proxy_http_version 1.1;<br> proxy_set_header Upgrade $http_upgrade;<br> proxy_set_header Connection "upgrade";<br> }<br>}
这里 proxy_pass 后的斜杠 【必须保留】,否则 /api/user 会被错误转发为 http://127.0.0.1:3001/api/user,而非预期的 http://127.0.0.1:3001/user —— 这是90%代理失效的根源。
保存后执行 sudo nginx -t 验证语法,无误则重载配置:sudo systemctl reload nginx。
处理OPTIONS预检请求(关键补丁)
方法一:独立location拦截OPTIONS(推荐)
在 codex.conf 的 server 块内、location /api/ { ... } 上方新增:
配置 OpenClaw 多提供商认证与 OpenAI Codex OAuth 备用方案,实现自动模型切换,适用于多 OpenAI Codex 账户设置。
location /api/ {<br> if ($request_method = 'OPTIONS') {<br> add_header Access-Control-Allow-Origin *;<br> add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";<br> add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With";<br> add_header Access-Control-Allow-Credentials "true";<br> add_header Access-Control-Max-Age 1728000;<br> add_header Content-Length 0;<br> add_header Content-Type text/plain;<br> return 204;<br> }<br>}
⚠️ 注意:这个 location 必须放在 proxy_pass 的 /api/ 块之前,否则Nginx会按字典序优先匹配 proxy_pass,导致OPTIONS仍被转发给后端并可能触发502。
方法二:用map模块实现Origin白名单(生产环境必需)
在 http 块顶层(即 /etc/nginx/nginx.conf 的 http { } 内)添加:
map $http_origin $cors_origin {<br> default "";<br> "http://localhost:3000" "http://localhost:3000";<br> "https://codex.example.com" "https://codex.example.com";<br>}
然后在 codex.conf 的 /api/ location 内加入:
add_header Access-Control-Allow-Origin $cors_origin;<br>add_header Access-Control-Allow-Credentials "true";
前端请求路径必须适配代理规则
第一步:打开Codex项目中的API调用代码,找到所有类似 fetch('http://api.example.com/v1/users') 或 axios.get('https://backend.com/api/data') 的硬编码URL。
第二步:全部替换为相对路径,例如:fetch('/api/users') 或 axios.get('/api/data')。
第三步:确保前端构建产物(如dist目录)已部署到Nginx root指定路径(本例为 /var/www/codex),且 index.html 中引用的JS/CSS路径为相对路径或根路径(以 / 开头)。
这一步不可跳过——如果前端仍发向原始后端地址,Nginx代理规则完全不生效,CORS报错照旧。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










