利用nginx增强elasticseach http安全性

为应对公司内部 Elasticsearch HTTP 端口的安全隐患,本文介绍了一种利用 Nginx 实现反向代理与 Basic Auth 的解决方案。通过将 Elasticsearch 绑定到本地地址并借助 Nginx 进行 SSL 加密及认证,有效提升了系统的安全性。

背景

最近公司安全检查,elasticsearch http端口处于开放状态,虽然在内网但依然风险很大.解决安全问题的套路基本上有两个:一个是官方的shield;还有一个是search guard.shield需要license,果断放弃.实际考察了一下search guard.东西不错但配置繁琐些,需要生产证书.而且不支持关闭node验证.公司大部分程序是java client开发的,这就意味着所有上线应用都要重新加证书才能正常工作,兄弟门改完了还得回归测试,成本有点高.于是想到了万能的nginx,反向代理+basic auth基本能够解决问题.


实施步骤

首先要修改elasticsearch.yml,加上下面一行来绑定http地址到127.0.0.1(只允许本地访问)

  1. http.host: 127.0.0.1

然后重启elasticsearch,现在9200端口只对本地开放,其他机器上访问全都被拒.

接下来配置nginx反向代理及ssl

生成ssl证书及密钥

  1. openssl req \
  2. -newkey rsa:4096 -nodes -sha256 -keyout eshttp.key \
  3. -x509 -days 3650 -out eshttp.crt


生成basic auth所需密码文件

  1. printf "admin:$(openssl passwd -crypt admin)\n" >>htpasswd

conf.d/upstream.conf

  1. upstream eslocalhttp {
  2. server 127.0.0.1:9200 max_fails=3 fail_timeout=4s weight=9;
  3. }


conf.d/open_eshttpproxy.conf

  1. server {
  2. server_name eshttproxy;
  3. listen 8080;
  4. ssl on;
  5. ssl_certificate /etc/nginx/conf.d/eshttp.crt;
  6. ssl_certificate_key /etc/nginx/conf.d/eshttp.key;
  7. location / {
  8. auth_basic "Restricted";
  9. auth_basic_user_file /etc/nginx/conf.d/htpasswd;
  10. proxy_redirect off;
  11. proxy_bind 127.0.0.1;
  12. proxy_pass http://eslocalhttp;
  13. }
  14. }

最后编写docker-compose.yml

  1. # For elasticsearch local http proxy
  2. version: '2'
  3. services:
  4. esproxy-nginx:
  5. image: nginx:alpine
  6. cpuset: '0'
  7. volumes:
  8. - /etc/localtime:/etc/localtime:ro
  9. - ./conf.d:/etc/nginx/conf.d
  10. network_mode: "host"
  11. container_name: esproxy-nginx

启动docker

  1. sudo docker-compse up -d

现在可以通过https://yourip:8080/ 访问elasticsearch的restful了

完整项目见:https://github.com/jiashiwen/eshttpproxy


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值