--- title: "HTTP CORS 允许源配置" date: 2026-03-13 lastmod: 2026-03-13 description: "控制 CORS 允许访问源的配置说明" tags: ["HTTP", "CORS", "跨域", "安全配置"] summary: "配置项作用 # http.cors.allow-origin 配置项指定允许通过跨域资源共享 (CORS) 访问 Easysearch 的源列表。 是否可选 # 是 默认值 # "" (空字符串,禁用 CORS) 配置项类型 # 静态配置 - 需要重启节点才能生效 配置格式 # # 禁用 CORS(默认) http.cors.allow-origin: "" # 允许所有源(不推荐用于生产) http.cors.allow-origin: "*" # 允许特定源 http.cors.allow-origin: "https://example.com" # 允许多个源(逗号分隔) http.cors.allow-origin: "https://example.com,https://app.example.com" # 使用正则表达式 http.cors.allow-origin: "/https:\/\/.*\.example\.com/" 格式说明 # 格式 说明 示例 空字符串 禁用 CORS "" 通配符 允许所有源 "*" 具体源 精确匹配 "https://example." --- ## 配置项作用 `http.cors.allow-origin` 配置项指定允许通过跨域资源共享 (CORS) 访问 Easysearch 的源列表。 ## 是否可选 是 ## 默认值 ``` "" (空字符串,禁用 CORS) ``` ## 配置项类型 **静态配置** - 需要重启节点才能生效 ## 配置格式 ```yaml # 禁用 CORS(默认) http.cors.allow-origin: "" # 允许所有源(不推荐用于生产) http.cors.allow-origin: "*" # 允许特定源 http.cors.allow-origin: "https://example.com" # 允许多个源(逗号分隔) http.cors.allow-origin: "https://example.com,https://app.example.com" # 使用正则表达式 http.cors.allow-origin: "/https:\/\/.*\.example\.com/" ``` ## 格式说明 | 格式 | 说明 | 示例 | |------|------|------| | 空字符串 | 禁用 CORS | `""` | | 通配符 | 允许所有源 | `"*"` | | 具体源 | 精确匹配 | `"https://example.com"` | | 多个源 | 逗号分隔 | `"https://a.com,https://b.com"` | | 正则表达式 | 模式匹配 | `/https:\/\/.*\.example\.com/` | ## 工作原理 ``` ┌─────────────────────────────────────────────────────────┐ │ CORS 请求验证 │ └─────────────────────────────────────────────────────────┘ 浏览器请求 │ │ Origin: https://app.example.com ▼ 检查 CORS 配置 │ ├── http.cors.enabled: false → 拒绝 │ └── http.cors.enabled: true │ ▼ 检查 allow-origin │ ├── "*" → 允许所有 ├── 精确匹配 → 允许 ├── 正则匹配 → 允许 └── 不匹配 → 拒绝 ``` ## 推荐设置 | 环境 | 推荐值 | 说明 | |------|--------|------| | 开发环境 | `"*"` | 便于调试 | | 生产环境 | 具体源列表 | 安全限制 | | 子域名 | 正则表达式 | 灵活匹配 | ## 使用示例 **开发环境配置:** ```yaml http.cors.enabled: true http.cors.allow-origin: "*" ``` **生产环境配置:** ```yaml http.cors.enabled: true http.cors.allow-origin: "https://app.example.com,https://admin.example.com" ``` **子域名配置:** ```yaml http.cors.enabled: true http.cors.allow-origin: "/https:\/\/.*\.example\.com/" ``` **配合凭证使用:** ```yaml http.cors.enabled: true http.cors.allow-origin: "https://app.example.com" http.cors.allow-credentials: true ``` ## 安全建议 **重要安全注意事项:** 1. **避免使用通配符与凭证** ```yaml # 危险组合 http.cors.allow-origin: "*" http.cors.allow-credentials: true # 浏览器会阻止此组合 # 正确做法 http.cors.allow-origin: "https://app.example.com" http.cors.allow-credentials: true ``` 2. **生产环境使用具体源** ```yaml # 推荐 http.cors.allow-origin: "https://app.example.com" # 避免 http.cors.allow-origin: "*" ``` 3. **使用 HTTPS** ```yaml # 确保源使用 HTTPS http.cors.allow-origin: "https://app.example.com" ``` ## 配置验证 ```bash # 查看当前配置 GET /_nodes/settings?filter_path=nodes.*.http.cors.allow-origin # 测试 CORS curl -X OPTIONS http://localhost:9200/ \ -H "Origin: https://app.example.com" \ -H "Access-Control-Request-Method: GET" \ -v ``` ## 常见问题 **问题 1:CORS 错误** **错误信息:** ``` Access to XMLHttpRequest has been blocked by CORS policy ``` **解决方案:** ```yaml http.cors.enabled: true http.cors.allow-origin: "https://your-app.com" ``` **问题 2:凭证未发送** **原因:** - 使用了 `*` 通配符 **解决方案:** ```yaml http.cors.allow-origin: "https://your-app.com" http.cors.allow-credentials: true ``` ## 注意事项 1. **静态配置**:修改配置需要重启节点 2. **凭证限制**:使用凭证时不能使用 `*` 3. **正则表达式**:用 `/ /` 包裹表示正则 4. **优先级**:需要在 `http.cors.enabled: true` 时生效 5. **浏览器限制**:CORS 由浏览器强制执行 ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `http.cors.enabled` | false | 是否启用 CORS | | `http.cors.allow-methods` | OPTIONS,HEAD,GET,POST,PUT,DELETE | 允许的方法 | | `http.cors.allow-headers` | X-Requested-With,Content-Type,Content-Length | 允许的头 | | `http.cors.allow-credentials` | false | 是否允许凭证 | | `http.cors.max-age` | 1728000 | 预检缓存时间 | ## 完整配置示例 ```yaml # easysearch.yml # 开发环境 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-credentials: false # 生产环境 http.cors.enabled: true http.cors.allow-origin: "https://app.example.com,https://admin.example.com" http.cors.allow-methods: "GET,POST,PUT,DELETE" http.cors.allow-headers: "Content-Type,Authorization" http.cors.allow-credentials: true http.cors.max-age: 1728000 ```