Nginx – Kibana4 + nginx reverse proxy using location /kibana4/ = Not Found 404

kibananginx

I'm trying to setup Kibana4 with nginx reverse proxy, with partial success so far. Here is my config related to Kibana:

server {
  listen 82;
  server_name ${HOSTNAME};

  set $kibana kibana.docker;

  location /kibana4/ {
    proxy_pass              http://$kibana:5601;
    proxy_set_header        Host $host;
    proxy_set_header        Referer "";
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version      1.1;
    proxy_connect_timeout   150;
    proxy_send_timeout      100;
    proxy_read_timeout      100;
    proxy_buffers           16 64k;
    proxy_busy_buffers_size 64k;
    client_max_body_size    256k;
    client_body_buffer_size 128k;
  }
}

In that case if I go to http://localhost:82/kibana4/ I will get Not Found 404

But if I replace location /kibana4/ with location /, everything work well – of course URL changes to http://localhost:82 in that case.

I have found a few other topics related to similar problem, e.g.:

Running sinatra program [Kibana] behind nginx reverse proxy in web directory

but the solutions described there don't work for me. I even tried this config:

server {
  listen 82;
  server_name ${HOSTNAME};

  set $kibana kibana.docker;

  location /kibana4 {
    proxy_pass              http://$kibana:5601/;
    proxy_set_header        Host $host;
    proxy_set_header        Referer "";
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version      1.1;
    proxy_connect_timeout   150;
    proxy_send_timeout      100;
    proxy_read_timeout      100;
    proxy_buffers           16 64k;
    proxy_busy_buffers_size 64k;
    client_max_body_size    256k;
    client_body_buffer_size 128k;
  }
}

It starts proxy to kibana.docker host, but the problem is that all URL are rewritten to the same kibana loading page. It causes that any CSS or JavaScript file cannot be loaded properly.

My question is: does anyone know what can be wrong here? Does anyone run successfully Kibana4 from a different than server root location? I would appreciate your help.

Best Answer

Thank you HD. for your question below my previous post - it was very enlightening. It helped me realize what was the problem. I always forget that location part is also passed to proxy, that's why it has to be rewritten in my case. Here is what the correct config should look like:

  location ~ ^/kibana4/(.*)$ {
    rewrite /kibana4/(.*) /$1  break;
    proxy_pass              http://$kibana:5601;
    proxy_set_header        Host $host;
    proxy_set_header        Referer "";
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version      1.1;
    proxy_connect_timeout   150;
    proxy_send_timeout      100;
    proxy_read_timeout      100;
    proxy_buffers           16 64k;
    proxy_busy_buffers_size 64k;
    client_max_body_size    256k;
    client_body_buffer_size 128k;
  }

It also explains why the location / setting is working correctly if there is no rewrite part. After adding rewrite, only the rest part is passed to proxy and it works like a charm.

Thank you once again, lesson learned :-)

Related Topic