charlie
charlie

Reputation: 181

Nginx + js server

I have a very simple js server (w/ webpack) serving a simple client-side app. My configuration file looks like this:

server {
    listen 80;

    root /home/user/project/dist;
    index index.html;

    server_name project.example.com;

    location / { 
        try_files $uri $uri/ =404;
    }
}

I have tried many combinations of try_files. I don't want it to 404 when it can't find something other than index.html (which is what it currently does). It works OK for the root (pointing the browser to project.example.com, but anything else gives a 404.

To solve this, I've tried to set try_files in many combinations, such as:

try_files $uri index.html; try_files $uri $uri/ index.html; try_files $uri /absoulute/path/to/index.html;

They either 404 everything or give a 500, which, according to nginx logs, is an infinite redirect loopback.

I don't think what I'm doing is terribly complex, but everything I've tried so far hasn't done what I want, which is to allow react-router to take care of everything by having try_files just give everything to index.html.

Any help appreciated.

Upvotes: 2

Views: 210

Answers (1)

Richard Smith
Richard Smith

Reputation: 49762

nginx URIs contain a leading /. If the URI http://project.example.com/index.html works, then the try_files statement should look like:

try_files $uri $uri/ /index.html;

See this document for details.

Upvotes: 1

Related Questions