How to convert try_files directive to yaml format?

1. The problem I’m having:

I want to represent it as a routing configuration similar to:

root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;

here’s my current code, but only the main page is visible and the subpages are not showing

- handle:
    - handler: subroute
      routes:
        - handle:
            - handler: file_server
              root: /frontend/dist
          match:
            - path:
                - /*

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

I believe YAML is a 1-to-1 translation from JSON. That means you can just take a Caddyfile and adapt it straight to JSON. From there you can rewrite it as YAML.

I did it like so:

http://example.com {
  root /usr/share/nginx/html
  try_files {path} {path}/ index.html
  file_server
}

I ran the command caddy adapt --config Caddyfile --pretty and got the following:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "example.com"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/usr/share/nginx/html"
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "rewrite",
                          "uri": "{http.matchers.file.relative}"
                        }
                      ],
                      "match": [
                        {
                          "file": {
                            "try_files": [
                              "{http.request.uri.path}",
                              "{http.request.uri.path}/",
                              "index.html"
                            ]
                          }
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "file_server",
                          "hide": [
                            ".\\Caddyfile"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    }
  }
}

From this I can see that, essentially, you want to put a file matcher with try_files in your subroute, with a rewrite handler. After that you can put your file_server handler or do whatever else you need.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.