0

We're needing something straightforward in concept, but not sure if nginx can handle.

The basic idea is to check if the path exists on the server...if so, pass the traffic through...if not, redirect with rewrite. For example.

Path: example.com/page/12345 <- this exists on server, so just pass through.

Path: example.com/this/7121 <- this doesn't exist on server, so pass to home.example.com/this/7121

Is this possible with nginx and without having a major impact on performance.

1 Answer 1

1

Sounds like the try_files directive is what you're looking for:

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

In your case, since you want your fallback to be a proxy, I believe you'll need to use a named location for your proxy. E.g.

location @fallback {
    proxy_pass https://home.example.com$uri;
}

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

Assuming that all you want to do when the requested path does exist locally is to serve that file as static content.

You must log in to answer this question.

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