5

I am setting up a Rails app with nginx in front.

What I want is first to check if the URL makes sense for Rails then serve content of the public folder.

I can't achieve this:

upstream extranet {
  server localhost:3000;
}

server {
  location / {
    try_files @extranet $uri;
    root /var/www/extranet/public;
  }

  location @extranet {
    proxy_pass          http://extranet;
    proxy_read_timeout  90;
    proxy_set_header  Host $host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  X-Forwarded-Ssl on;
    proxy_set_header  X-Forwarded-Port $server_port;
    proxy_set_header  X-Forwarded-Host $host;
    client_max_body_size 100m;
  }
}

I get: *1 rewrite or internal redirection cycle while internally redirecting to "/" error.

It seems like try_files $uri @extranet; works but in my case it feels safer to check the Rails app first because the public folder might change.

Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206

1 Answers1

8

try_files checks for the presence of a file on the local file system and cannot respond to the response code from a proxy.

Presumably, the proxy response with a 404 response if the remote page does not exist, which can be intercepted by an error_page statement.

For example:

location / {
    proxy_pass       http://extranet;
    proxy_set_header ...
    proxy_set_header ...
    proxy_set_header ...

    proxy_intercept_errors on;
    error_page 404 = @fallback;
}
location @fallback {
    root /var/www/extranet/public;
    try_files $uri =404;
}

See this document for more.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81