Elasticsearch无法通过Curl访问localhost:9200问题求助
Elasticsearch curl返回Empty reply但浏览器可访问的问题排查
我已经成功启动Elasticsearch服务器,通过浏览器访问localhost:9200完全正常,但尝试用curl执行以下一系列命令时,全部返回错误:curl: (52) Empty reply from server。
执行的curl命令:
curl -u elastic:H5VzhJP0txg4Lm9EMSZp localhost:9200curl -u elastic:H5VzhJP0txg4Lm9EMSZp http://localhost:9200curl -u elastic:H5VzhJP0txg4Lm9EMSZp http://127.0.0.1:9200curl -XGET -u elastic:H5VzhJP0txg4Lm9EMSZp http://localhost:9200curl -XGET -u elastic:H5VzhJP0txg4Lm9EMSZp -H 'Content-Type: application/json' 'http://localhost:9200'
Elasticsearch版本信息:
☁ kibana-research [master] ⚡ elasticsearch --version Java HotSpot(TM) 64-Bit Server VM warning: Cannot open file logs/gc.log due to No such file or directory Version: 6.2.4, Build: ccec39f/2018-04-12T20:37:28.497551Z, JVM: 1.8.0_171
排查建议:
结合浏览器正常访问、curl报错的情况,大概率是两者的运行环境差异导致,你可以按以下步骤逐一排查:
绕过代理测试
很多时候curl会默认使用系统代理,而浏览器可能配置了直接访问localhost。试试在curl命令中添加--noproxy "*"参数强制绕过所有代理:curl --noproxy "*" -u elastic:H5VzhJP0txg4Lm9EMSZp http://localhost:9200强制使用IPv4
部分系统中localhost会优先解析到IPv6地址(::1),如果Elasticsearch仅监听了IPv4的127.0.0.1,就会出现curl请求失败。添加-4参数强制curl使用IPv4:curl -4 -u elastic:H5VzhJP0txg4Lm9EMSZp http://localhost:9200检查Elasticsearch绑定配置
打开Elasticsearch配置文件config/elasticsearch.yml,确认以下配置:network.host:是否设置为127.0.0.1、localhost或0.0.0.0(允许所有IP访问),如果设为其他特定IP,可能导致localhost无法访问;http.port:是否保持默认的9200,没有被修改为其他端口。
排查本地安全软件拦截
有些防火墙、杀毒软件会对命令行工具的网络请求做特殊拦截,暂时关闭这类软件后再测试curl命令,看是否能正常返回结果。测试curl版本与认证方式
- 先检查curl版本:
curl --version,如果是较旧版本,升级后再重试; - 换一种认证方式测试(仅用于排查):
curl http://elastic:H5VzhJP0txg4Lm9EMSZp@localhost:9200
- 先检查curl版本:
内容的提问来源于stack exchange,提问作者Lin Du




