docker - Nginx位置/日志未正确传递给Kibana,导致404

标签 docker nginx kibana nginx-location

我们有一个dockerized架构。唯一且主要的入口点是我们的nginx。这使得我们所有服务之间建立了联系。

使用/位置到达域名可以正常工作。我们的Angular前端应用正确显示。

主要问题是在/logs位置上访问KIBANA(v5.5)。

例如,尝试达到https://dev.example.com/logs/时,浏览器会显示404,这是因为它试图达到https://dev.example.com/login?next=%2Flogs

似乎在我们的Location端点中代理的Kibana尝试使用/的基本URL重写URL。而不是重写/ logs /位置后面附加的URI。

如何将我们的配置改进为:
-允许/ logs /正确显示我们的Kibana应用程序?

这是我们的nginx配置。请注意,客户端,后端,kibana是指docker主机名。

server {
    listen  443;
    ssl     on;
    rdns    on;

    ssl_certificate /etc/ssl/production/certs/example/fullchain.pem;
    ssl_certificate_key /etc/ssl/production/certs/example/privkey.pem;

    server_name dev.example.com;

    # Angular APP
    location / {
        proxy_pass http://client;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Backend proxy
    location /api {
        proxy_pass http://backend:9090;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Logs on Kibana
    location /logs {
        proxy_pass http://kibana:5601;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

CURL请求的结果:
curl -v https://dev.example.com/logs/
* About to connect() to dev.example.com port 443 (#0)
*   Trying xx.xx.xx.xx...
* connected
* Connected to dev.example.com (xx.xx.xx.xx) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*        subject: CN=example.com
*        start date: 2017-08-23 17:26:00 GMT
*        expire date: 2017-11-21 17:26:00 GMT
*        subjectAltName: dev.example.com matched
*        issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*        SSL certificate verify ok.
> GET /logs HTTP/1.1
> User-Agent: curl/7.28.1
> Host: dev.example.com
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: nginx/1.13.1
< Date: Sat, 26 Aug 2017 15:39:43 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
<
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.13.1</center>
</body>
</html>
* Connection #0 to host dev.example.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):

感谢您的协助。

最佳答案

您需要通过将server.basePath环境变量传递到kibana容器来将/logs设置为SERVER_BASEPATH

然后,您需要在代理位置使用重写来去除nginx中的/ logs前缀:

location /logs {
    rewrite ^/logs(/.*)$ $1 break;
    proxy_pass http://kibana:5601;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

另外,显然在kibana中存在一个错误,并且仅当我们使用以下网址访问kibana时,以上配置才有效:
http://nginxip/logs/

但如果没有结尾斜杠,则不会,例如:
http://nginxip/logs

为了解决这个问题,我们需要在nginx中添加另一个重写,以确保始终存在一个斜杠。在/ logs位置之外添加以下内容:
rewrite ^/logs$ /logs/;

资料来源:https://www.elastic.co/guide/en/kibana/current/settings.html

关于docker - Nginx位置/日志未正确传递给Kibana,导致404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895733/

相关文章:

java - Elastic Beanstalk .ebextensions 在 WAR 中被忽略

docker - 以分离模式启动时如何检查docker-compose日志?

Docker 容器到 NAS 存储

linux - 使用 Docker (NVIDIA Xavier Jetson) 在 ARM64 上安装 Parity

node.js - 在 nginx (https) 中托管多个服务器 block

nginx - 如何通过 Nginx 代理 RDP

elasticsearch - Elasticsearch查询聚合仅具有特定模式的术语

elasticsearch - LogStash: kibana Alert - No results 没有结果,因为没有找到与您选择的时间跨度相匹配的索引

bash - 如何快速切换docker环境进行开发?

elasticsearch - 在 Kibana 上访问 _meta(在 _mapping 中)