'NGINX ignores newlines in log file

I am trying to emit the output of a bash script to index.html and to be displayed in nginx.

This is the script itself-

#!/bin/bash
set +e

TIMEFORMAT='%3R'
script_runtime=$(date +%s)

APP_ID="test"
folder_put_time="1"
folder_get_time="2"
folder_ls_time="3"
folder_ls_count="4"

out=$(jq --arg key0 'script_runtime{application="'$APP_ID'"}' \
   --arg value0 "$script_runtime"  \
   --arg key1   'folder_put_time{application="'$APP_ID'"}' \
   --arg value1 "$folder_put_time" \
   --arg key2   'folder_get_time{application="'$APP_ID'"}' \
   --arg value2 "$folder_get_time" \
   --arg key3   'folder_ls_time{application="'$APP_ID'"}' \
   --arg value3 "$folder_ls_time" \
   --arg key4   'folder_ls_count{application="'$APP_ID'"}' \
   --arg value4 "$folder_ls_count" \
   '. | .[$key0]=$value0 | .[$key1]=$value1 | .[$key2]=$value2 | .[$key3]=$value3 | .[$key4]=$value4' \
   <<<'{}' )

file=$(jq -r 'keys_unsorted[] as $k | "\($k) \(.[$k])"' <<< $out)

echo "$file"
echo "$file" >> /somepath/index.html 

Which produces this output-

user@mbp-user tests % cat index.html 
script_runtime{application="test"} 1648547558
folder_put_time{application="test"} 1
folder_get_time{application="test"} 2
folder_ls_time{application="test"} 3
folder_ls_count{application="test"} 4

However, displayed at NGINX with no newlines at all.

How can I solve this?

After reading the comment, this is the config in my nginx /etc/nginx/sites-enabled folder

server {
    listen 8081;
        location /metrics {
        alias /logsfolder;
        autoindex on;
    }
}

Youre suggesting to add the include there?

It is displayed properly while accesing the file directly, but I need it to be displayed on the /metrics/ url without the file name http://hostname:8081/metrics/index.log

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source