📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

配置项作用 #

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.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
        │
        ├── "*" → 允许所有
        ├── 精确匹配 → 允许
        ├── 正则匹配 → 允许
        └── 不匹配 → 拒绝

推荐设置 #

环境推荐值说明
开发环境"*"便于调试
生产环境具体源列表安全限制
子域名正则表达式灵活匹配

使用示例 #

开发环境配置:

http.cors.enabled: true
http.cors.allow-origin: "*"

生产环境配置:

http.cors.enabled: true
http.cors.allow-origin: "https://app.example.com,https://admin.example.com"

子域名配置:

http.cors.enabled: true
http.cors.allow-origin: "/https:\/\/.*\.example\.com/"

配合凭证使用:

http.cors.enabled: true
http.cors.allow-origin: "https://app.example.com"
http.cors.allow-credentials: true

安全建议 #

重要安全注意事项:

  1. 避免使用通配符与凭证
# 危险组合
http.cors.allow-origin: "*"
http.cors.allow-credentials: true  # 浏览器会阻止此组合

# 正确做法
http.cors.allow-origin: "https://app.example.com"
http.cors.allow-credentials: true
  1. 生产环境使用具体源
# 推荐
http.cors.allow-origin: "https://app.example.com"

# 避免
http.cors.allow-origin: "*"
  1. 使用 HTTPS
# 确保源使用 HTTPS
http.cors.allow-origin: "https://app.example.com"

配置验证 #

# 查看当前配置
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

解决方案:

http.cors.enabled: true
http.cors.allow-origin: "https://your-app.com"

问题 2:凭证未发送

原因:

  • 使用了 * 通配符

解决方案:

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.enabledfalse是否启用 CORS
http.cors.allow-methodsOPTIONS,HEAD,GET,POST,PUT,DELETE允许的方法
http.cors.allow-headersX-Requested-With,Content-Type,Content-Length允许的头
http.cors.allow-credentialsfalse是否允许凭证
http.cors.max-age1728000预检缓存时间

完整配置示例 #

# 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