How to Configure Nginx Reverse Proxy for Kibana?


Introduction

In today's world, data is the backbone of every industry. To make sense of it all, tools like Kibana are used to visualize and analyze the data.

However, running Kibana on its own can often lead to performance issues and security vulnerabilities. This is where Nginx reverse proxy comes in handy.

Preparing the Environment for Nginx Reverse Proxy Configuration

Installing Nginx on the Server

Before installing Nginx on the server, it is important to ensure that all necessary dependencies are installed. This can be done by running a system update and then installing the dependencies using the package manager of your distribution.

Once these are installed, you can proceed with installing Nginx itself. The installation process may vary depending on your distribution, but in general, it involves adding an Nginx repository to your system and then running a command to install it.

Setting up a Domain Name and SSL Certificate

In order to use HTTPS with your reverse proxy configuration, you will need to set up a domain name and SSL certificate for your website. This involves obtaining a domain name from a registrar such as GoDaddy or Namecheap, and then obtaining an SSL certificate from a provider such as LetsEncrypt or Comodo. Once you have both of these things, you can configure Nginx to use them in your reverse proxy configuration.

Installing Kibana on the Server

Kibana is built using Node.js and requires it as a dependency. Therefore, before installing Kibana itself, we must first install Node.js on our server if it is not already present. This can be done by adding the relevant repository for your distribution and then running an installation command.

After this is complete, we can proceed with downloading Kibana itself from its official website and extracting it onto our server. We should then create a symlink or move it into our desired installation directory so that we can easily reference it in our configuration file later on.

Configuring Nginx as a Reverse Proxy for Kibana

Creating an Nginx configuration file

The first step in configuring Nginx as a reverse proxy for Kibana is to create an Nginx configuration file. This file will contain the necessary settings that will allow Nginx to act as a reverse proxy for Kibana.

The configuration file can be created using any text editor, such as vi or nano, and should be saved with the .conf extension. Once the file has been created, it should be placed in the /etc/nginx/conf.d/ directory.

Adding proxy settings to the configuration file

After creating the configuration file, the next step is to add the appropriate proxy settings to it. The following example shows how to configure Nginx to act as a reverse proxy for Kibana: ```

server { listen 80;

server_name domain.com; location / {

proxy_pass http://localhost: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;

auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; } } ```

This configuration sets up an HTTP server on port 80 that listens for requests from domain.com and proxies them to Kibana running on http://localhost:5601/. It also sets up basic authentication using an .htpasswd file located at /etc/nginx/.htpasswd.

Testing the configuration

Once you have added the necessary settings to your configuration file, you need to test your new setup. To do this, you can use curl or your web browser of choice and visit your domain name (e.g. http://domain.com).

If everything is working properly, you should see the Kibana interface displayed in your browser. Configuring Nginx as a reverse proxy for Kibana can be a bit tricky, but with careful planning and attention to detail, it is possible to set up an effective and secure reverse proxy configuration that enables remote users to access Kibana resources with ease.

Advanced Configuration Options

Using authentication with Nginx and Kibana

By default, Kibana does not include any built-in authentication mechanism. However, to secure access to your Kibana instance, you can use Nginx's built-in basic HTTP authentication feature. Basic HTTP authentication allows you to set a username and password for accessing your Kibana instance.

To enable basic HTTP authentication, you will need to modify the Nginx configuration file for your Kibana proxy. In the server block for your domain name, add the following lines−

auth_basic "Restricted Access"; auth_basic_user_file /etc/nginx/.htpasswd;   

The first line sets a message that will be displayed when users are prompted for their credentials. The second line specifies the password file that contains encrypted usernames and passwords.

To create the .htpasswd file with a user account and password, use the `htpasswd` command as follows −

sudo htpasswd -c /etc/nginx/.htpasswd exampleuser     

The `-c` option creates a new file if it doesn't exist yet. Replace `exampleuser` with any username of your choice.

Load balancing with multiple instances of Kibana

If you're running multiple instances of Kibana in a cluster environment, it's recommended to use Nginx as a load balancer. Load balancing distributes incoming network traffic across different server instances to optimize resource usage and improve availability.

To configure Nginx as a load balancer for multiple instances of Kibana, modify your Nginx configuration file as follows −

upstream kibanapool { 
server 10.0.0.1:5601; server 10.0.0.2:5601; } 
server { listen 80; 
server_name your-domain.com; location / { 
proxy_pass http://kibanapool; proxy_set_header Host $host; } }  

The `upstream` block defines a group of Kibana instances and their IP addresses. The `server` block then specifies the listener port and domain name, along with the `proxy_pass` setting that forwards requests to the upstream group.

Caching static files with Nginx

Nginx can also cache static files such as images, CSS stylesheets, and JavaScript scripts to improve performance. Caching allows frequent or repeated requests for the same file to be served from memory instead of reading from disk each time. To enable caching for static files in Nginx, modify your configuration file as follows −

http { ... # Define a path for where to store cached data on disk 
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; ... server { 
listen 80; server_name your-domain.com; 
location / { # Enable caching for all types of static files 
proxy_cache my_cache; proxy_cache_valid 200 60m; # Cache responses with HTTP status code 200 OK for up to 60 minutes 
add_header X-Cache-Status $upstream_cache_status; # Add a header indicating whether a response was served from cache proxy_pass http://localhost:5601; # Forward requests to Kibana } ... } }  

The `proxy_cache_path` directive specifies a directory path where cached data will be stored on disk. The `proxy_cache` directive enables caching for all types of static files.

The `proxy_cache_valid` directive sets how long responses should be cached based on their HTTP status code. The `add_header` directive adds a custom header to indicate whether a response was served from cache.

Conclusion

Configuring Nginx as a reverse proxy for Kibana can greatly enhance the performance and security of your web applications. By redirecting traffic through Nginx, you can take advantage of its advanced caching and load balancing capabilities to improve the user experience. Additionally, using SSL encryption with an SSL certificate can help protect sensitive data from potential security breaches.

Updated on: 11-Jul-2023

800 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements