Nginx 80端口转发隐藏真实ElasticSearch地址端口

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")));

猜你喜欢

转载自blog.csdn.net/weixin_43430036/article/details/83756185