0

I am setting up nginx as a sort of static file server. For some reason it is only working when I go to 123.123.123.123/ or 123.123.123.123. However, when I go to 123.123.123.123/static/content/ or 123.123.123.123/static/content/another.mp3 it returns a 404 not found. Here is the config file that is located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. I am really stumped as to why it is not working.

Any pointers or tips would be appreciated.

server {
    listen 123.123.123.123:80;
    server_name "";

     location / {
         root /srv/homepage;
         index  index.html;
     }

     location /static/content/ {
         root /srv/static/content;
         index  song.mp3;
     }

}
2
  • Could you verify that 123.123.123.123/ really returns /srv/homepage/index.html, and that /srv/static/content/song.mp3 really exists? What do the log files say? May 29, 2012 at 7:26
  • check your error_log, there should be more detailed error message.
    – DukeLion
    May 29, 2012 at 9:09

1 Answer 1

1

If you look in your error log, you'll see that a request for 123.123.123.123/static/content/another.mp3, nginx will look for /srv/static/content/static/content/another.mp3. This is due to the way that the root directive works in nginx. When using root, nginx will append the request uri path to the given root, so you'll get one /static/content from the root directive, and another from the request. The correct approach is to just set your root to /srv:

location /static/content/ {
  root /srv;
  index song.mp3;
}

Since all requests handled by this location will start with /static/content, it will only server files out of /srv/static/content.

3
  • thanks for the answer. I added the setting to get the log files and it was going to /srv/static/content/static/content/another.mp3. The answer you gave works. In the future I might want to move the files on the server and not change the URI. Is there a way to just get the name of the song and then append that to a place on the file system using nginx?
    – Scoop
    May 29, 2012 at 17:20
  • 1
    If I understand you correctly, you'd want to use an alias in that case. If you moved the files to /srv/blah instead of /srv/static/content, then it'd look like this: location /static/content/ { alias /srv/blah/; }. When using an alias, you'll need to ensure that any trailing / in the location is matched in the alias or nginx will go looking for the wrong files.
    – kolbyjack
    May 29, 2012 at 17:57
  • Yes. Excellent. alias will work perfectly!!
    – Scoop
    May 29, 2012 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .