avatarM Taufiq Permana S

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6423

Abstract

an> <span class="hljs-attr">prefix:</span> <span class="hljs-string">index_</span> <span class="hljs-attr">period:</span> <span class="hljs-string">24h</span>

<span class="hljs-attr">storage_config:</span> <span class="hljs-attr">boltdb_shipper:</span> <span class="hljs-attr">active_index_directory:</span> <span class="hljs-string">/loki/boltdb-shipper-active</span> <span class="hljs-attr">cache_location:</span> <span class="hljs-string">/loki/boltdb-shipper-cache</span> <span class="hljs-attr">cache_ttl:</span> <span class="hljs-string">24h</span> <span class="hljs-comment"># Can be increased for faster performance over longer query periods, uses more disk space</span> <span class="hljs-attr">shared_store:</span> <span class="hljs-string">filesystem</span> <span class="hljs-attr">filesystem:</span> <span class="hljs-attr">directory:</span> <span class="hljs-string">/loki/chunks</span>

<span class="hljs-attr">compactor:</span> <span class="hljs-attr">working_directory:</span> <span class="hljs-string">/loki/boltdb-shipper-compactor</span> <span class="hljs-attr">shared_store:</span> <span class="hljs-string">filesystem</span>

<span class="hljs-attr">limits_config:</span> <span class="hljs-attr">reject_old_samples:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">reject_old_samples_max_age:</span> <span class="hljs-string">168h</span> <span class="hljs-attr">ingestion_burst_size_mb:</span> <span class="hljs-number">16</span> <span class="hljs-attr">ingestion_rate_mb:</span> <span class="hljs-number">16</span>

<span class="hljs-attr">chunk_store_config:</span> <span class="hljs-attr">max_look_back_period:</span> <span class="hljs-string">0s</span>

<span class="hljs-attr">table_manager:</span> <span class="hljs-attr">retention_deletes_enabled:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">retention_period:</span> <span class="hljs-string">90d</span> <span class="hljs-comment"># Set retention period to 90 days</span>

<span class="hljs-attr">ruler:</span> <span class="hljs-attr">storage:</span> <span class="hljs-attr">type:</span> <span class="hljs-string">local</span> <span class="hljs-attr">local:</span> <span class="hljs-attr">directory:</span> <span class="hljs-string">/loki/rules</span> <span class="hljs-attr">rule_path:</span> <span class="hljs-string">/loki/rules-temp</span> <span class="hljs-attr">alertmanager_url:</span> <span class="hljs-string">localhost</span> <span class="hljs-attr">ring:</span> <span class="hljs-attr">kvstore:</span> <span class="hljs-attr">store:</span> <span class="hljs-string">inmemory</span> <span class="hljs-attr">enable_api:</span> <span class="hljs-literal">true</span></pre></div><p id="2066">4. Install and Configure Nginx</p><div id="7cae"><pre>apt-get install nginx</pre></div><p id="f5cd">5. Create loki nginx config</p><div id="2e07"><pre>sudo nano /etc/nginx/sites-available/<LOKI_DOMAIN_NAME>.conf</pre></div><div id="1321"><pre>server { <span class="hljs-keyword">listen</span> <span class="hljs-number">80</span>; server_name <LOKI_DOMAIN_NAME>; <span class="hljs-comment">#Repalce with your domain name</span>

location / {
    proxy_pass http:<span class="hljs-regexp">//l</span>ocalhost:<span class="hljs-number">3100</span>; <span class="hljs-comment">#match with running loki port number</span>
    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;
}

}

server { <span class="hljs-keyword">listen</span> <span class="hljs-number">80</span>; server_name <LOKI_DOMAIN_NAME>; <span class="hljs-comment">#Repalce with your domain name</span>

location / { proxy_read_timeout <span class="hljs-number">1800</span>s; proxy_connect_timeout <span class="hljs-number">1600</span>s; proxy_pass http:<span class="hljs-regexp">//l</span>ocalhost:<span class="hljs-number">3100</span>; <span class="hljs-comment">#match with running loki port number</span> proxy_http_version <span class="hljs-number">1.1</span>; proxy_set_header Upgrade http_upgrade; proxy_set_header Connection connection_upgrade; proxy_set_header Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_set_header Proxy-Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_redirect off; }

location /ready { proxy_pass http:<span class="hljs-regexp">//l</span>ocalhost:<span class="hljs-number">3100</span>; <span class="hljs-comment">#match with running loki port number</span> proxy_http_version <span class="hljs-number">1.1</span>; proxy_set_header Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_set_header Proxy-Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_redirect off; auth_basic <span class="hljs-string">"off"</span>; } }</pre></div><div id="19be"><pre>sudo <span class="hljs-built_in">ln</span> -s /etc/nginx/sites-available/<LOKI_DOMAIN_NAME>.conf /etc/nginx/sites-enabled/</pre></div><p id="4678">7. Configure nginx HTTPS with certbot</p><div id="35b0"><pre>sudo snap install --classic certbot sudo <span class="hljs-built_in">ln</span> -s /snap/bin/certbot /usr/bin/certbot sudo certbot --nginx</pre></div><figure id="41a4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*zqF4oJIOqFwc0qGm.png"><figcaption><a href="https://linuxblog.io/how-to-set-up-an-nginx-certbot/">How to Set Up an Nginx Certbot</a></figcaption></figure><p id="ef21">8. Setup Loki HTTP Basic Auth</p><div id="72fe"><pre>sudo apt-get install apache2-utils sudo htpasswd -c /etc/nginx/.loki_htpasswd <span class="hljs-tag"><<span class="hljs-name">LOKI_USERNAME</span>></span> >>> Enter Password <span class="hljs-tag"><<span class="hljs-name">LOKI_PASSWORD</span>></span></pre></div><p id="8dc0">9. Configure Loki HTTP Basic Auth Nginx Config File</p><div id="056a"><pre>server { server_name <LOKI_DOMAIN_NAME>;

auth_basic <span class="hljs-string">"loki auth"</span>; auth_basic_user_file /etc/nginx/.loki_htpasswd;

location / { proxy_read_timeout 1800s; proxy_connect_timeout 1600s; proxy_pass http://localhost:3100; proxy_http_versio

Options

n 1.1; proxy_set_header Upgrade <span class="hljs-variable">http_upgrade</span>; proxy_set_header Connection <span class="hljs-variable">connection_upgrade</span>; proxy_set_header Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_set_header Proxy-Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_redirect off; }

location /ready { proxy_pass http://localhost:3100; proxy_http_version 1.1; proxy_set_header Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_set_header Proxy-Connection <span class="hljs-string">"Keep-Alive"</span>; proxy_redirect off; auth_basic <span class="hljs-string">"off"</span>; }

listen 443 ssl; <span class="hljs-comment"># managed by Certbot</span>
ssl_certificate /etc/letsencrypt/live/&lt;LOKI_DOMAIN_NAME&gt;/fullchain.pem; <span class="hljs-comment"># managed by Certbot</span>
ssl_certificate_key /etc/letsencrypt/live/&lt;LOKI_DOMAIN_NAME&gt;/privkey.pem; <span class="hljs-comment"># managed by Certbot</span>
include /etc/letsencrypt/options-ssl-nginx.conf; <span class="hljs-comment"># managed by Certbot</span>
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; <span class="hljs-comment"># managed by Certbot</span>

} server { <span class="hljs-keyword">if</span> (<span class="hljs-variable">host</span> = &lt;LOKI_DOMAIN_NAME&gt;) { <span class="hljs-built_in">return</span> 301 https://host<span class="hljs-variable">$request_uri</span>; } <span class="hljs-comment"># managed by Certbot</span>

listen 80; server_name <LOKI_DOMAIN_NAME>; <span class="hljs-built_in">return</span> 404; <span class="hljs-comment"># managed by Certbot</span></pre></div><h1 id="e3e1">B.) Promtail Installation and Configuration</h1><ol><li>Add Grafana Repository</li></ol><div id="bce1"><pre><span class="hljs-meta prompt_"># </span><span class="language-bash"><span class="hljs-built_in">mkdir</span> -p /etc/apt/keyrings/</span> <span class="hljs-meta prompt_"># </span><span class="language-bash">wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor > /etc/apt/keyrings/grafana.gpg</span> <span class="hljs-meta prompt_"># </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main"</span> | <span class="hljs-built_in">tee</span> /etc/apt/sources.list.d/grafana.list</span></pre></div><p id="1026">2. Install Promtail</p><div id="6d15"><pre>apt<span class="hljs-operator">-</span><span class="hljs-keyword">get</span> <span class="hljs-keyword">update</span> apt<span class="hljs-operator">-</span><span class="hljs-keyword">get</span> install promtail systemd <span class="hljs-keyword">start</span> promtail <span class="hljs-operator">&&</span> systemd enable promtail systemd status promtail</pre></div><p id="8ea2">3. Configure Promtail Configuration File (Example)</p><div id="09b0"><pre><span class="hljs-built_in">mv</span> /etc/promtail/promtail.yaml /etc/promtail/promtail.yaml.bak sudo nano /etc/promtail/promtail.yaml</pre></div><div id="2edf"><pre><span class="hljs-attr">server:</span> <span class="hljs-attr">http_listen_port:</span> <span class="hljs-number">9080</span> <span class="hljs-attr">grpc_listen_port:</span> <span class="hljs-number">0</span>

<span class="hljs-attr">positions:</span> <span class="hljs-attr">filename:</span> <span class="hljs-string">/tmp/positions.yaml</span>

<span class="hljs-comment">#Replace loki username, password and domain name</span> <span class="hljs-attr">clients:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">url:</span> <span class="hljs-string">https://<LOKI_DOMAIN_NAME>/loki/api/v1/push</span> <span class="hljs-attr">basic_auth:</span> <span class="hljs-attr">username:</span> <span class="hljs-string"><LOKI_USERNAME></span> <span class="hljs-attr">password:</span> <span class="hljs-string"><LOKI_PASSWORD></span>

<span class="hljs-attr">scrape_configs:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">job_name:</span> <span class="hljs-string">system</span> <span class="hljs-attr">static_configs:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">targets:</span> <span class="hljs-bullet">-</span> <span class="hljs-string">localhost</span> <span class="hljs-attr">labels:</span> <span class="hljs-attr">job:</span> <span class="hljs-string">varlogs</span> <span class="hljs-attr">path:</span> <span class="hljs-string">/var/log/*log</span> <span class="hljs-attr">stream:</span> <span class="hljs-string">stdout</span></pre></div><h1 id="3af3">C.) Configuration Test</h1><p id="f882">For testing the configuration is correct you can use curl command inside promtail instance</p><div id="9f15"><pre>curl -i -u <<span class="hljs-variable constant_">LOKI_USERNAME</span>><span class="hljs-symbol">:<LOKI_PASSWORD></span> <span class="hljs-symbol">https:</span>/<span class="hljs-regexp">/<LOKI_DOMAIN_NAME>/loki</span><span class="hljs-regexp">/api/v</span>1/labels</pre></div><p id="5c41">If everything is setup correctly you should see this output below:</p><div id="d213"><pre>HTTP/1.1 200 OK Server: nginx/1.18.0 (Ubuntu) Date: Tue, 06 Aug 2024 08:24:20 GMT Content-Type: application/json; charset=UTF-8 Content-Length: 71 Connection: keep-alive Vary: Accept-Encoding

{<span class="hljs-string">"status"</span>:<span class="hljs-string">"success"</span>,<span class="hljs-string">"data"</span>:[<span class="hljs-string">"filename"</span>,<span class="hljs-string">"job"</span>,<span class="hljs-string">"service_name"</span>,<span class="hljs-string">"stream"</span>]}</pre></div><h1 id="a149">Conclusion</h1><p id="a779">By following these steps, you’ve successfully secured your Grafana Loki instance with basic authentication behind an Nginx reverse proxy. This provides a solid foundation for protecting your log data.</p><p id="fa16"><b>Reference:</b></p><ul><li><a href="https://dev.to/ruanbekker/running-loki-behind-nginx-reverse-proxy-1699">https://dev.to/ruanbekker/running-loki-behind-nginx-reverse-proxy-1699</a></li><li><a href="https://linuxblog.io/how-to-set-up-an-nginx-certbot/">https://linuxblog.io/how-to-set-up-an-nginx-certbot/</a></li></ul></article></body>

Setup Grafana Loki Basic Auth Behind Nginx Reverse Proxy (HTTPS)

Quick Introduction

Grafana Loki, a powerful and scalable log aggregation system, lacks built-in authentication. To protect your log data, it’s essential to implement a robust authentication mechanism. This post guides you through setting up basic authentication for Loki behind an Nginx reverse proxy.

Promtail is a log collection agent designed to efficiently gather log data from various sources and send it to a Grafana Loki instance for storage and analysis. It’s a crucial component of the Grafana Loki stack, working alongside Loki and Grafana to create a comprehensive log management solution.

Monitoring and logging are crucial aspects of maintaining the health and performance of your applications. Grafana Loki, a powerful tool for log aggregation and visualization, simplifies this task. By setting it up behind an Nginx reverse proxy with basic authentication, you can secure your logging infrastructure efficiently. This guide will walk you through the entire process step by step.

Grafana Loki Basic Auth Behind Nginx Reverse Proxy (HTTPS) Diagram

Why Use Nginx as a Reverse Proxy?

Nginx is a high-performance web server and reverse proxy renowned for its efficiency and flexibility. By placing Nginx in front of Loki, you can:

  • Enhance security: Implement basic authentication to restrict access to your log data.
  • Load balancing: Distribute traffic across multiple Loki instances.
  • SSL termination: Secure communication with HTTPS.
  • Caching: Improve performance by caching static content.

Prerequisites

Required Software and Tools

Before we begin, ensure you have the following:

  • A Linux server (Ubuntu/Debian preferred)
  • Root or sudo access
  • Grafana Loki and Grafana binaries
  • Nginx installed
  • Basic understanding of Linux command-line operations

A.) Loki Installation and Configuration

  1. Add Grafana Repository
# mkdir -p /etc/apt/keyrings/
# wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor > /etc/apt/keyrings/grafana.gpg
# echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | tee /etc/apt/sources.list.d/grafana.list

2. Install Loki

apt-get update
apt-get install loki
systemd start loki && systemd enable loki
systemd status loki

3. Configure Loki Configuration File (Example)

mv /etc/loki/local-config.yaml /etc/loki/local-config.yaml.bak
sudo nano /etc/loki/local-config.yaml
auth_enabled: false

#By default loki running on port 3100
server:
  http_listen_port: 3100 

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 1h       # Any chunk not receiving new logs in this time will be flushed
  max_chunk_age: 1h           # All chunks will be flushed when they hit this age, default is 1h
  chunk_target_size: 1048576  # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
  chunk_retain_period: 30s    # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
  max_transfer_retries: 0     # Chunk transfers disabled

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

storage_config:
  boltdb_shipper:
    active_index_directory: /loki/boltdb-shipper-active
    cache_location: /loki/boltdb-shipper-cache
    cache_ttl: 24h         # Can be increased for faster performance over longer query periods, uses more disk space
    shared_store: filesystem
  filesystem:
    directory: /loki/chunks

compactor:
  working_directory: /loki/boltdb-shipper-compactor
  shared_store: filesystem

limits_config:
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  ingestion_burst_size_mb: 16
  ingestion_rate_mb: 16

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: true
  retention_period: 90d # Set retention period to 90 days

ruler:
  storage:
    type: local
    local:
      directory: /loki/rules
  rule_path: /loki/rules-temp
  alertmanager_url: localhost
  ring:
    kvstore:
      store: inmemory
  enable_api: true

4. Install and Configure Nginx

apt-get install nginx

5. Create loki nginx config

sudo nano /etc/nginx/sites-available/<LOKI_DOMAIN_NAME>.conf
server {
    listen 80;
    server_name <LOKI_DOMAIN_NAME>; #Repalce with your domain name

    location / {
        proxy_pass http://localhost:3100; #match with running loki port number
        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;
    }
}

server {
  listen 80;
  server_name <LOKI_DOMAIN_NAME>; #Repalce with your domain name

  location / {
    proxy_read_timeout 1800s;
    proxy_connect_timeout 1600s;
    proxy_pass http://localhost:3100; #match with running loki port number
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
    proxy_redirect off;
  }

  location /ready {
    proxy_pass http://localhost:3100; #match with running loki port number
    proxy_http_version 1.1;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
    proxy_redirect off;
    auth_basic "off";
  }
}
sudo ln -s /etc/nginx/sites-available/<LOKI_DOMAIN_NAME>.conf /etc/nginx/sites-enabled/

7. Configure nginx HTTPS with certbot

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx
How to Set Up an Nginx Certbot

8. Setup Loki HTTP Basic Auth

sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.loki_htpasswd <LOKI_USERNAME>
>>> Enter Password <LOKI_PASSWORD>

9. Configure Loki HTTP Basic Auth Nginx Config File

server {
  server_name <LOKI_DOMAIN_NAME>;

  auth_basic "loki auth";
  auth_basic_user_file /etc/nginx/.loki_htpasswd;

  location / {
    proxy_read_timeout 1800s;
    proxy_connect_timeout 1600s;
    proxy_pass http://localhost:3100;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
    proxy_redirect off;
  }

  location /ready {
    proxy_pass http://localhost:3100;
    proxy_http_version 1.1;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header Proxy-Connection "Keep-Alive";
    proxy_redirect off;
    auth_basic "off";
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/<LOKI_DOMAIN_NAME>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<LOKI_DOMAIN_NAME>/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = <LOKI_DOMAIN_NAME>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen 80;
  server_name <LOKI_DOMAIN_NAME>;
    return 404; # managed by Certbot

B.) Promtail Installation and Configuration

  1. Add Grafana Repository
# mkdir -p /etc/apt/keyrings/
# wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor > /etc/apt/keyrings/grafana.gpg
# echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | tee /etc/apt/sources.list.d/grafana.list

2. Install Promtail

apt-get update
apt-get install promtail
systemd start promtail && systemd enable promtail
systemd status promtail

3. Configure Promtail Configuration File (Example)

mv /etc/promtail/promtail.yaml /etc/promtail/promtail.yaml.bak
sudo nano /etc/promtail/promtail.yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

#Replace loki username, password and domain name
clients:
  - url: https://<LOKI_DOMAIN_NAME>/loki/api/v1/push
    basic_auth:
      username: <LOKI_USERNAME>
      password: <LOKI_PASSWORD>

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      __path__: /var/log/*log
      stream: stdout

C.) Configuration Test

For testing the configuration is correct you can use curl command inside promtail instance

curl -i -u <LOKI_USERNAME>:<LOKI_PASSWORD> https://<LOKI_DOMAIN_NAME>/loki/api/v1/labels

If everything is setup correctly you should see this output below:

HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Tue, 06 Aug 2024 08:24:20 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 71
Connection: keep-alive
Vary: Accept-Encoding

{"status":"success","data":["filename","job","service_name","stream"]}

Conclusion

By following these steps, you’ve successfully secured your Grafana Loki instance with basic authentication behind an Nginx reverse proxy. This provides a solid foundation for protecting your log data.

Reference:

Grafana
Grafana Loki
Grafana Promtail
Linux
Log Monitoring
Recommended from ReadMedium