目标:
我想在 docker 容器中运行 elk 堆栈。能够通过nginx代理访问ELK堆栈,以绕过服务的各个端口。
Kibana 服务(默认端口 5601(
http://<server>.com:5601
应可通过以下地址联系:
http://<server>.com/kibana
问题:
问题是,在将 server.basePath 设置添加到配置后,无法访问 kibana 站点。只有当我将 Kibana 的每个基本 api 调用添加到 nginx 配置(/api、/ui、...(时,我才能启动该服务。
配置:
Kibana 的配置:
/opt/kibana/config/kibana.yml
包含以下条目:
server.host: "0.0.0.0"
server.basePath: "/kibana"
其他一切都是默认的
多库server.basePath
# Enables you to specify a path to mount Kibana at if you are running behind a proxy. This only affects # the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests # to Kibana. This setting cannot end in a slash.
nginx配置:
location /kibana/ {
rewrite ^/kibana(/.*)$ $1 break;
proxy_pass http://<server>.com:5601/;
}
我使用 sebp/elk:551 docker 镜像和以下 docker-compose 文件:
version: '2'
services:
elk:
image: sebp/elk:551
container_name: "elk"
volumes:
- /etc/kibana/config/kibana.yml:/opt/kibana/config/kibana.yml
ports:
- "5601:5601"
- "9200:9200"
- "5044:5044"
environment:
SERVICE_5601_NAME: "kibana"
SERVICE_9200_NAME: "elasticsearch"
SERVICE_5044_NAME: "logstash"
restart: always
我尝试过的:
我已经在 Kibana 4.6.1 中尝试了相同的设置,它按预期完美运行。
我测试过但不起作用的版本:5.4.3、5.1.2、5.0.2
我不想要的:
我不想像/api, /ui, /app/kibana, ...
一样添加 Kibana 的每个子目录以添加到代理配置中。
是否有其他解决方案或版本?
编辑1:@whites11:浏览器从nginx返回502 Bad Gateway站点。 浏览器信息:
常规
Request URL:http://<server-name>.com/kibana/
Request Method:GET
Status Code:502 Bad Gateway
Remote Address:<server-ip>:80
Referrer Policy:no-referrer-when-downgrade
响应标头
Connection:keep-alive
Content-Length:575
Content-Type:text/html
Date:Thu, 24 Aug 2017 13:54:49 GMT
Server:nginx/1.13.3
请求标头
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:<server-name>.com
Upgrade-Insecure-Requests:1
来自nginx的日志
34#34: *8 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: <IP>, server: , request: "GET /kibana/ HTTP/1.1", upstream: "http://<server-ip>:5601/", host: "<server-name>.com"
我没有与您完全相同的部署,但我使用的是 dockerized kibana。
首先,在nginx设置中需要以下内容:
location /kibana/ {
proxy_pass http://kibana_server:5601/;
}
根据您的环境更改值,但最后的斜杠至关重要!不要删除它们!他们确保重写是按照 Kibana 的预期完成的,即,从转到 kibana 服务器的请愿书中的 URL 中删除kibana
。
然后保持以下内容:
server.basePath: /kibana
在 Kibana 设置中。这可确保 kibana 提供的文档(链接和 URL(具有前缀/kibana
.
它对我有用。
我花了几个小时来解决这个问题。基本上,如果您在nginx后面运行kibana,则应将环境变量设置为SERVER_BASEPATH=/yourpath。
nginx配置:
upstream kibana {
server kibana:5601;
}
server {
listen 80;
location /kibana/ {
rewrite /kibana/(.*) /$1 break;
proxy_pass http://kibana/;
}
}
Kibana 在 docker-compose 中的设置:
kibana:
image: kibana:7.7.1
ports:
- "5601:5601" # Important: In a production environment remove external port
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
- SERVER_BASEPATH=/kibana
depends_on:
- elasticsearch
networks:
- backend
注意:elasticsearch 是 docker-compose 文件中的另一个服务。
有关更多详细信息,请检查实现情况:https://github.com/Jamaxack/Kangaroo
首先,接触 Kibana 是没有意义的,因为我们无论如何都会使用 Nginx 作为反向代理。所以抛弃你的服务器.basePath
接下来在下面更改您的nginx配置哟
location /kibana/ {
proxy_pass http://<server>.com:5601/;
}
这意味着当您访问http://<nginxhost>:<port>/kibana/xyz/abc
.这相当于使用http://<server>.com:5601/xyz/abc
.消除系统的任何复杂性
编辑-1
对于那些认为这不起作用的人来说,事实并非如此。这是我在发布此答案之前设置的示例测试用例。
events {
worker_connections 1024;
}
http {
server {
listen 80;
location /test1 {
proxy_pass http://127.0.0.1:81;
}
location /test2 {
proxy_pass http://127.0.0.1:81/;
}
location /test3/ {
proxy_pass http://127.0.0.1:81;
}
location /test4/ {
proxy_pass http://127.0.0.1:81/;
}
}
server {
listen 81;
location / {
echo "$request_uri";
}
}
}
现在结果解释了所有 4 个位置块之间的区别
$ curl http://192.168.33.100/test1/abc/test
/test1/abc/test
$ curl http://192.168.33.100/test2/abc/test
//abc/test
$ curl http://192.168.33.100/test3/abc/test
/test3/abc/test
$ curl http://192.168.33.100/test4/abc/test
/abc/test