1. The problem I’m having:
Hi there,
I’m very new to caddy, so apologise in advance if I ask stupid questions.
I’m writing an internal url shortener that will be used for intranet only use.
Looking at caddy’s functionalities, this seems very trivial.
However, I found multiple ways to achieve the same thing, like for example:
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"path": [
"/qwerty"
]
}
],
"handle": [
{
"@id": "qwerty",
"handler": "static_response",
"headers": {
"Location": [
"https://google.com"
]
},
"status_code": 302
},
{
"handler": "metrics",
"disable_openmetrics": false
}
]
}
]
}
}
}
}
}
or
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443",
":80"
],
"routes": [
{
"match": [
{
"host": [
"foo.local"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"destinations": [
"{redirect-uri}"
],
"handler": "map",
"mappings": [
{
"input": "/tube",
"outputs": [
"https://youtube.com"
]
},
{
"input": "/photos",
"outputs": [
"https://google.com"
]
},
{
"input": "/rickrolled",
"outputs": [
"https://www.youtube.com/watch?v=dQw4w9WgXcQ"
]
}
],
"source": "{http.request.uri.path}"
}
]
},
{
"handle": [
{
"handler": "static_response",
"headers": {
"Location": [
"{redirect-uri}"
]
},
"status_code": 302
}
],
"match": [
{
"expression": "{redirect-uri} != \"\""
}
]
},
{
"handle": [
{
"body": "Thas's an unknown short URL ... :(",
"handler": "static_response"
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}
From here I got the redirect working in both ways, but this raised me several questions:
- Which one is the most appropriate way, considering that these short urls are dynamic and updated from another service constantly?
- How can I build a “catch-all” handler that redirect to a specific page if users provide an unknown path?
- I need to extract several metrics like unique users per link, remote ip, various data inferred from user-agent, etc., how can I achieve that? Can I send an event from caddy to a MQ instance or to a custom server that perform the aggregation of these metrics? If yes, how can I do that? Or what is the right way to accomplish that?
2. Error messages and/or full log output:
no errors
3. Caddy version:
2.7.6
4. How I installed and ran Caddy:
For now with brew
a. System environment:
mac os