HAProxy community

Unable to use custom message for 404 error

Hi,

I am able to set custom error files for all errors except 404 error using errorfile and errorloc to redirect error page.

Can someone help me to set custom error page for 404 errrors.
Best Regards,
Tejesh

Unfortunately, at the moment, setting custom 404 errors is not possible.

I’ve written, earlier this year, an email about exactly this use-case to the HAProxy mailing list:
https://www.mail-archive.com/haproxy@formilux.org/msg32761.html
(To my knowledge, no conclusion was reached, nor any action was taken.)

Hi Ciprian,

Thanks for responding regarding this issue.

I think we have to raise a feature request for the same in GitHub.

Let me know your thoughts.

Best Regards,
Tejesh

Just adding support for 404 and nothing else would be quite trivial, just a matter of grep and copy-paste.

My initial proposal was to add support for arbitrary error codes.

Thus when opening the feature request we’ll have to think about how “extensive” the feature request would be.

Thus, do you think you need support for other non-standard status codes, or just 404?

Hi Ciprian,

I think as of now, we need 404 status codes and also it will be great if the support is enabled for other non-standard status codes.

Best Regards,
Tejesh

Then you need to weigh in your opinion on the matter with the developers… (I would suggest re-opening the thread I’ve pointed to above.)

Alternatively someone needs to sponsor the development of this feature. :slight_smile:

FWIW the mailing list thread does contain a workaround. It’s hackish, but it’s something that works today.

Without filing an feature request on github, this will almost certainly be forgotten (or already is).

I strongly suggest to file a feature request on github, if only to track user demand.

The work-around implies using the generic 400 error code as 404. Which means that any error that HAProxy considers to be a client error will translate to 404.

I didn’t read the thread, but for custom responses you’d usually create a dedicated backend without any servers and rewrite the 500 error code.

You’d definitely don’t have to rewrite existing error codes impacting the normal error responses.

edit: here’s a custom backend, returning a robots.txt file based on error 403 (but only for this backend, so nothing else is affected):

backend robots
        mode http
        errorfile 403 /etc/haproxy/errors/robotstxt.http
        http-request deny

The point is: use dedicated backends.

Come on… Granted “technically” it works, but it’s a long way from a “proper” solution…


(As a sidenote, not strictly related to this, it starts to seem to me that many things that should be trivial to achieve in HAProxy tend to imply “workarounds”… For example multiple stick tables, using deny in responses with any other status than 503, properly using the dir match as the documentation states to match “directories” not “sub-directories”, creating a variable with “log-fmt” syntax, which involves writing a header, copying it to the variable, and then deleting the header, being able to set custom headers on redirects, regular expressions that contain / which happens to be the path separator, etc…)

I did not claim it’s a proper solution, in fact I called it hackish.

I merely clarified that you are not “sacrificing” an error code for the entire instance.