-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify status code of reverse proxy response #2920
Comments
Hi, welcome to the issue tracker! I am not entirely sure that I understand your post. Do you have 2 questions here?
|
Arguably, you'd say that those configurations should be the responsibility of the upstreams you are passing the request to, i.e. a proxy would only act as a middle-man in a request transaction. |
Hmmm no I believe this should be possible with Caddy 2, without imperative syntax -- if it's not possible already, then we should implement it. I just need to know exactly what needs to be implemented. So first, we need some clarification from @hackwaly. |
I need to use config to achieve:
|
I almost understand -- can you give an actual example of one such configuration you're trying to accomplish? |
BTW: I already wrote a go program to get what i want. Config may like this: {
"apps": {
"proxy_app": {
"servers": {
"proxy_server": {
"routes": [
{
"handle": [
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "{http.request.header.X-Host}"
}
],
"headers": {
"handler": "headers",
"response": {
"delete": ["Set-Cookie"],
"set": {
"X-Set-Cookie": ["{http.handlers.reverse_proxy.response.header.Set-Cookie}"],
"Access-Control-Allow-Origin": ["*"]
},
"set_status_code": "{http.handlers.reverse_proxy.response.status_code >= 400 ? 500 : 200}"
}
}
}
],
"terminal": true
}
]
}
}
}
}
} |
@hackwaly Neat, is the source anywhere? (Reopening until we implement it here) |
Ping @hackwaly -- how did you implement that feature? |
@mholt |
Chiming in here since I want to feature that is in the title even though the issue kind of derailed: My use case is the following. I have a MinIO backend where I serve a static website from. In case it responds with 404 I have an error handler that rewrites the request to The problem is that this will result in a 200 status code for the 404 page which is arguably not ideal. So overwrite the status code would be great. |
@fahrradflucht So, in your use case, you don't actually want to overwrite the status code, but you just want Caddy's error handler routes to be invoked on whatever error status is returned from the backend, right? Kind of like nginx's |
Maybe I don't fully understand you, but I think I actually want to overwrite the status code. Let me make an example. Given a Caddyfile like this: example.com {
handle_errors {
@404 {
expression {http.error.status_code} == 404
}
rewrite @404 /404.html
}
reverse_proxy https://minio.example.com {
header_up Host {http.reverse_proxy.upstream.hostport}
}
} The Does that make sense? Or am I approaching this totally wrong? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@fahrradflucht Is the backend originating the 404 error? I.e. does |
Yes, exactly. |
@fahrradflucht Okay, then you don't actually want to "overwrite" the status code, you want Caddy to handle the response from the backend as an error, rather than passing it through to the client. |
This is possible with commit 7a99835 |
@fahrradflucht Sorry for hijacking this issue but I am trying to use the exact same setup (Caddy reverse proxying to MinIO) and have everything working but custom 404 pages. I tried using the snippet you provided here:
but it's not having any effect. Did you ever get that setup working? For the record, this is my complete config:
|
FYI #4021 is merged, Caddyfile support will be in v2.4.0. |
Sorry to comment in an old issue, but this seemed to be the best place for this. I am in the same situation as @fahrradflucht's comment above: I am reverse proxying to a minio backend serving a static site. This site has a custom However, this feels a little messy so I was wondering if there was a better approach, perhaps involving
|
@wlcx that's the correct way to do it. For next time, please ask your usage questions on the Caddy community forums. We prefer to keep the GitHub issue board for bugs and feature requests. Don't forget to fill out the thread template so we can help you! |
In the future, we could probably introduce some convenience wrapper over that syntax to make it a little simpler to serve custom error pages. |
Could it be done by only config?
And are there reverse_proxy response placeholders? Such as headers, status_code, body etc.
The text was updated successfully, but these errors were encountered: