Score:0

Serve file using nginx web server

cw flag

Hi I have static file (song.mp3 under /song directory in the VM) that I would like to serve under path /song using nginx web server. If anybody visits the path in browser, the file should start downloading.

I added the following info in the config file

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /song {
        root /song/song.mp3;
    }
}

However when I start the nginx service and use the path http://localhost/song it throws 404 not Found. Any pointers on what might be causing this?

rvs avatar
vn flag
rvs
Did you check the logs for any errors?
user89978 avatar
cw flag
@rvs This is the error: 023/04/01 13:47:37 [error] 23#23: *2 open() "/song/song.mp3/song" failed (20: Not a directory), client: localhost, server: localhost, request: "GET /song HTTP/1.1", host: "localhost"
Score:0
mt flag

Hi you can try the following below

server
{
    listen 80;
    listen [::]:80;
    server_name localhost;

    location /
    {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }

    location /song/
    {
        try_files $uri /song/$uri;
    }
}

Try to use try_files. The $uri will map /song/ directory with /song/ & when you will hit the url http://localhost/song/song.mp3 it will start download.

Also make sure song.mp3 exists in /usr/share/nginx/html/song folder.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.