使用nginx控制ElasticSearch訪問權限

介紹

因爲ElasticSearch自己沒有權限管理模塊,只要獲取服務器的地址和端口,任何人均可以隨意讀寫ElasticSearch的API並獲取數據,這樣很是不安全。因此本文就介紹一下如何規避這種問題,實現方式其實有幾種,本文將介紹使用nginx的basic_auth來控制的方式。html

  • 好處 使用nginx的好處是輕便,不用涉及到Elasticsearch 或者 Kibana自己的配置上,
  • 壞處 可被暴力破解

環境

Centos 6.9 Elasticsearch 版本 6.4.0 Kibana 版本 6.4.0 Nginx 版本 1.11.6nginx

安裝nginx

  1. 下載nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.11.6.tar.gz
複製代碼
  1. 解壓ngxin
tar -zxvf nginx-1.11.6.tar.gz 
複製代碼
  1. 進入nginx目錄
cd nginx-1.11.6
複製代碼
  1. 執行configure檢查配置
./configure
複製代碼
4.1. 發現錯誤, 缺乏 pcre 包
```
./configure: error: the HTTP rewrite module requires the PCRE library.
複製代碼

You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option. ``` vim

  1. 安裝pcre包
yum -y install pcre-devel
複製代碼
  1. 再次執行configure檢查配置
./configure
複製代碼
6.1 發現還缺乏一個zlib的包
```
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
```
複製代碼
  1. 安裝zlib包
yum -y install zlib-devel
複製代碼
  1. 執行configure
./configure
複製代碼
  1. 執行make install
make install
複製代碼
  1. 進入nginx目錄啓動nginx
cd  /usr/local/nginx/
複製代碼

啓動nginx瀏覽器

./sbin/ngxin
複製代碼

能夠進入你的瀏覽器輸入你的ip地址看看是否能訪問安全

配置ngxin

下面就開始對ngxin的配置文件作修改,修改/conf目錄下面的nginx.confbash

vim conf/nginx.conf
複製代碼

將裏面的服務器

location / {
            root   html;
            index  index.html index.htm;
        }
複製代碼

改爲ui

location / {
            proxy_pass  http://0.0.0.0:5601;
            auth_basic "登錄驗證";
            auth_basic_user_file /usr/local/nginx/htpasswd;
        }
複製代碼

而後使用htpasswd命令生成密碼文件spa

[root@test102 conf.d]# htpasswd -cm /usr/local/nginx/htpasswd kibana #/usr/local/nginx/htpasswd就是配置文件裏面配置的密碼文件,kibana就是用戶名
New password:     #輸入密碼
Re-type new password:     #再次輸入密碼,回車
Adding password for user crystal
複製代碼

進入瀏覽器訪問你的ip,會讓你輸入用戶名和密碼才能進入kibana了code

輸入用戶名和密碼

點擊登錄就進入了kibana

相關文章
相關標籤/搜索