ElasticSearch 通过nginx做HTTP验证

在ElasticSearch 的设置文件中如果设置了

network.host: 0.0.0.0

则表示ElasticSearch 服务是公开的任何ip都可以访问ElasticSearch 服务。这样肯定是不安全的。

我们可以通过安装 X-Pack这个然间来做对ElasticSearch 的登陆验证,但是这个是收费的只可以免费使用30天。

还有一种方法也是我们常用就是使用nginx的反向代理服务同时使用Http-basic模块来做HTTP验证。

在nginx 下添加配置文件内容如下

 

#upstream dev.es.daojia.com.cn {
#        //如果有多台服务器可以在这里配置upstream轮询
#     }

server{
        server_name dev.es.xxx.com.cn;
        location /{
            proxy_http_version 1.1;
            proxy_set_header   Connection          "";
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

            auth_basic "login";
            auth_basic_user_file /etc/nginx/conf.d/htpasswd;
            autoindex on;

            proxy_pass http://127.0.0.1:9200; 
        }
        access_log  /data/logs/nginx/dev.es.xxx.com.cn.access.log  main;
    error_log   /data/logs/nginx/nginx-error.log;

}
proxy_pass 表示代理目标
auth_basic_user_file 表示账号密码存放的文本地址 
通过http://www.matools.com/htpasswd 来生成采用Crypt (all Unix servers)  方式密的密码

 

 




 可以看到 用户名为明文密码为加密后的用:分隔

将其复制到我们设置的目录文件中。

然后修改ElasticSearch 的配置network.host

如果nginx和ElasticSearch 在同一个服务器上可以设置为

network.host: 127.0.0.1

如果不在同一个机器上就将  network.host选项设置为nginx服务器的ip 就可以了。

然后重启nginx 和 ElasticSearch 

然后我们在通过ip加端口访问 ElasticSearch 就无法访问了。

在输入我们配置的域名看一下

输入账号es123和密码es123 就可以了

posted @ 2018-03-19 10:55  奋进程序猿  阅读(6648)  评论(0编辑  收藏  举报