1

I am trying to set a location in the configuration that allows me to do something like https://example.com/car/<vin> which would not go to a <vin> application or directory but to /car/index.html. From there, I would read the URL or pass <vin> to /car/index.html.

I have tried various regex location blocks, like the one below, but they all result in a 404 when accessing /car/<vin>.

location ~ ^/car/(.*)$ {
    root $document_root/car/
    index index.html;
}

What would be an appropriate location block?

1 Answer 1

1

Do you want to use a regular expression? The prefix location would also work as it matches any URI that begins with /car/. See this document for details.

For example:

location ^~ /car/ {
    try_files /car/index.html =404;
}

Using $document_root in the root statement may not work, and the index directive only works with URIs that end with a /. The try_files statement is probably the simplest solution. See this document for details.

Sign up to request clarification or add additional context in comments.

2 Comments

This works for me. I just had to add an if check to make sure it would only work for /car/<vin> and not something like /car/<vin>/wheels: if ($request_uri !~ "^/car/[A-Z0-9]+$") { return 404; }.
You could use a regular expression location instead, but they are evaluated in order, so be aware of potentially conflicting locations elsewhere in the server block. For example: location ~ ^/car/[A-Z0-9]+$ { try_files /car/index.html =404; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.