5

I have a single page web application. I want to set up nginx to serve files under a folder directly, and for all other requests, I want it to serve a single html file. The setup I wanna achieve is as follows:

.
.
.
location /static/ {
    # Serve directly here
}

location / {
    # Serve single file here (index.html), without changing the path
}
.
.
.

Answers to similar questions I found suggested using a url rewrite, but as far as I know, that would redirect the user to a new url, hence changing the path.

What I want is, whatever the user types except something beginning with static, nginx serves the index.html file without changing the path, because I will be loading dynamic content to the webpage using the path variable.

11

Simplest way would be

location / {
    try_files /index.html /index.html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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