This repository provides an example setup for configuring Grafana Loki, along with a React application demonstrating how to send logs to Loki. Additionally, it includes a basic Nginx configuration to serve as a reverse proxy for accessing Loki.
-
Install Grafana Loki: Follow the official Grafana Loki installation guide to set up Loki on your server.
-
Configure Loki: Customize your Loki configuration according to your requirements. The configuration file is typically named
loki-local-config.yaml
. -
Start Loki: Run the Loki instance using the following command:
loki --config.file=loki-local-config.yaml
-
Clone this repository:
git clone https://github.com/example/react-loki-logs.git cd react-loki-logs
-
Install dependencies:
npm install
-
Configure Loki Endpoint: Open the
.env
file and set theREACT_APP_LOKI_ENDPOINT
variable to the Loki HTTP endpoint. -
Run the React App:
npm start
This will start the React app, and you can explore how logs are sent to Loki.
Assuming you have Nginx installed, add the following server block to your Nginx configuration file (e.g., /etc/nginx/sites-available/default
):
server {
listen 80;
server_name localhost;
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization, Accept, Origin, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Content-Range, Range';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH';
add_header 'Content-Type' 'application/json';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization, Accept, Origin, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Content-Range, Range';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH';
proxy_pass http://loki:3100;
}
}