25

In my application I want the location "/" to return a static index.html file, I want "/static" to serve static files from a folder and I want all other requests to return 404 NOT FOUND. At a later point I'm going to redirect all other requests to a WSGI server.

This is currently my configuration:

# Dev server.
server {
    listen 1337;
    charset UTF-8;

    location / {
        rewrite ^ static/index_debug.html break;
    }

    location /static {
        alias /home/tomas/projects/streamcore/static;
    }
}

The static folder is working perfectly, but "/" is returning 404 NOT FOUND. I have also tried:

alias /home/tomas/projects/streamcore/static/index_debug.html;

In the location block, but that returns 500 INTERNAL SERVER ERROR. It seems like alias doesn't like single files. Additionally I've tried:

try_files static/index_debug.html;

But that stops the server from starting with the error "invalid number of arguments in try_files directive". Apparently try_files actually requires you to try more than one file, which is not the behaviour I'm looking for.

So my question is: How so I configure a location block to always return a static file?


EDIT: I've seen from other answers that alias should indeed accept a single file as argument, so I tried:

location = / {
    alias /home/tomas/projects/streamcore/static/index_debug.html;
}

But I still only get 500 INTERNAL SERVER ERROR. The error log for the "/" request says:

[alert] 28112#0: *7 "/home/tomas/projects/streamcore/static/index_debug.htmlindex.html" is not a directory

Why is it trying to open "index_debug.htmlindex.html"? I'm not using the index directive anywhere.

5
  • Can you try_files static/index_debug.html static/index_debug.html;
    – ceejayoz
    Commented Feb 6, 2013 at 21:52
  • @ceejayoz: Trying your suggestion with location =/ I get 404 NOT FOUND, with location / I get 500 INTERNAL SERVER ERROR.
    – Hubro
    Commented Feb 6, 2013 at 21:56
  • If I'm not wrong, you're missing the Index directive... Any webserver will always look for the index file, providing the root URI (/), i think you can override this by adding: location / { return 404; } Commented Feb 6, 2013 at 22:19
  • @MartinoDino: Why do I need it? And where?
    – Hubro
    Commented Feb 6, 2013 at 22:21
  • location = / {Index /home/tomas/projects/streamcore/static/index_debug.html;} Commented Feb 6, 2013 at 22:38

3 Answers 3

18

Just tested this and it works for me:

server {
    root /tmp/root;
    server {
        listen 8080;
        location /static {
            try_files $uri =404;
        }
        location / {
            rewrite ^ /static/index_debug.html break;
        }
    }
}

The file /tmp/root/static/index_debug.html exists of course :)

I can hit any URL and I just get the static page.

2

You can use try_files and use an HTTP code, e.g. 404, as the second argument:

try_files /static/index_debug.html =404;

Note the leading slash.

The complete example would look like this (you don't need the rewrite):

# Dev server.
server {
    listen 1337;
    charset UTF-8;
    root /home/tomas/projects/streamcore/;

    location / {
        try_files /static/index_debug.html =404;
    }
}
0

You can use try_files and a redirect to a named location with rewrite. Like this:

server {
    root /tmp/root;
    server {
        listen 8080;

        location / {
            try_files $uri $uri/ @index;
        }

        location @index {
            rewrite ^ static/index_debug.html break;
        }
    }
}

If file exists in /tmp/root it will be served, otherwise uri will be rewritten to static/index_debug.html and it will be served.

You must log in to answer this question.

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