Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
     }

}
share|improve this question
    
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? –  Antonis Christofides May 29 '12 at 7:26
    
check your error_log, there should be more detailed error message. –  DukeLion May 29 '12 at 9:09
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 17:57
    
Yes. Excellent. alias will work perfectly!! –  Scoop May 29 '12 at 18:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.