5

I've got the following Nginx config:

server {
  listen   80;
  server_name  mercury;

  access_log  /var/log/nginx/mercury.access.log;
  error_log   /var/log/nginx/mercury.error.log;

  location /static {
    add_header Cache-Control: max-age=31536000;
  }

  location / {
    root   /opt/the-jam/www/dist/;
    try_files $uri /index.html;
    add_header Cache-Control: max-age=60;
  }
}

And I've got the directory structure:

§ tree /opt/the-jam/www/dist
/opt/the-jam/www/dist
├── index.html
└── static
    ├── 3522b60dabd4468d03f8.css
    └── 3522b60dabd4468d03f8.js

And I'm getting the error:

2015/10/20 14:25:26 [error] 4529#0: *95 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 0.0.0.0, server: the-jam, request: "GET /favicon.ico HTTP/1.1", host: "the-jam.example.com", referrer: "http://the-jam.example.com/"

This is a single-page app where any request, i.e. /foo/bar/baz should just load /index.html, unless it's requesting something in /static/[hash].js, so my understanding is that the try_files directive will try load the file at /foo/bar/baz, and then fall back to /index.html, so why am I getting the redirection cycle?

1
  • 1
    root should be under server, not location. Oct 20, 2015 at 15:17

1 Answer 1

9

The problem with your config is that if /index.html cannot be found, it will redirect to /index.html. Such configurations are better to avoid, even if you are sure that the file is here. A configuration like this don't have this problem:

root /opt/the-jam/www/dist/;

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

location = /index.html {
    # no try_files here
    ...
}

With such configuration you also will be able to see what's wrong with /index.html and why it can't be accessed. My best guess is that access rights on some intermediate directories doesn't allow nginx to access /opt/the-jam/www/dist/index.html.

You must log in to answer this question.

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