Nginx 80端口轉發隱藏真實elasticsearch地址端口
一般來講,如果我們直接將elasticsearch的地址和端口暴露出去,容易被攻擊,爲安全起見,我們可以利用Nginx代理分發隱藏elasticsearch地址信息。
這裏需要注意,我們使用的是Nginx的Http協議反向代理,可以對所有支持Http協議的操作(例如 elasticsearch),但是不是http協議的不可以,例如Redis.
es.conf
我們可以在Nginx的站點目錄下配置es.conf
vim /usr/local/nginx/sites-enabled/es.conf
添加以下信息
upstream es {
server 127.0.0.1:9200;
}
server {
listen 80;
server_name es.example.cn;
access_log /data/logs/nginx/es.access.log main;
error_log /data/logs/nginx/es.error.log error;
location / {
allow 127.0.0.1;
#允許特定IP訪問
allow 47.100.176.104;
allow 118.168.36.58;
#禁止其他IP訪問,防止攻擊
deny all;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://es;
proxy_read_timeout 120;
proxy_ignore_client_abort on;
}
}
Reload Nginx
/usr/local/nginx/sbin/nginx -s reload
kibana.conf
同理,我們可以在Nginx的站點目錄下配置kibana.conf,對kibanan做同樣的處理。
vim /usr/local/nginx/sites-enabled/kibana.conf
添加以下信息
upstream kibana {
server 127.0.0.1:5601;
}
server {
listen 80;
server_name kibana.example.cn;
access_log /data/logs/nginx/kibana.access.log main;
error_log /data/logs/nginx/kibana.error.log error;
location / {
allow 127.0.0.1;
#允許特定IP訪問
allow 47.100.176.104;
allow 118.168.36.58;
#禁止其他IP訪問,防止攻擊
deny all;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://kibana;
proxy_read_timeout 120;
proxy_ignore_client_abort on;
}
}
Reload Nginx
/usr/local/nginx/sbin/nginx -s reload
/etc/hosts
配置Host信息(或者配置真實二級域名)
vim /etc/hosts
添加以下信息:
118.118.118.118(Nginx遠程主機IP) es.example.cn
118.118.118.118(Nginx遠程主機IP) kibana.example.cn
測試遠程連接
瀏覽器訪問 kibana.example.cn
JAVA High Client 連接
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("es.example.cn", 80, "http")));