Answers: <p>You can make use of http-errors for creating different error code and file mappings.</p>
  <p>After that, you can easily make use of <code>http-response</code> in haproxy config to return based on the error code. An example is below:</p>
  <pre><code>http-errors site1
      errorfile 404 /etc/haproxy/errors/site1-404.http
  
  http-errors site2
      errorfile 404 /etc/haproxy/errors/site2-404.http
  
  frontend allsites
      bind :80
      default_backend site1-servers
      use_backend site2-servers if { req.hdr(host) site2.com }
  
      # Store host header in variable
      http-request set-var(txn.host) req.hdr(host)
  
      # Use site1 error page if site1.com
      http-response return status 404 errorfiles site1 if { status 404 } { var(txn.host) site1.com }
  
      # Use site2 error page if site2.com
      http-response return status 404 errorfiles site2 if { status 404 } { var(txn.host) site2.com }
  </code></pre>
  <p>You can also dynamically generate different 404 error pages from a single template by passing the request parameters from haproxy to the error template.
  I would recommend to go through this <a href="https://www.haproxy.com/blog/serve-dynamic-custom-error-pages-with-haproxy/" rel="nofollow noreferrer">haproxy blog</a> to understand in detail and try it out.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-06-25T21:41:41.797Z
Answers: 2021-06-25T21:41:41.797Z
Answers: Samit
Body: <p>My goal is to build custom 404 http error code that will depend on a user's language. I thought that I can use header accept language.</p>
  <p>I thought about something like this:</p>
  <p><strong>#creating 2 acls that accepts only one accept language.</strong></p>
  <pre><code>acl en req.fhdr(accept-language),language(en;pl) -m str en
  acl pl req.fhdr(accept-language),language(en;pl) -m str pl
  </code></pre>
  <p><strong>#and here i would like to use backends depends on acl's</strong></p>
  <pre><code>use_backend polish404 if pl
  use_backend english404 if en
  
  backend polish404
    mode http
    errorfile 503 /path/to/file
  
  backend english404
    mode http
    errorfile 503 /path/to/file
  </code></pre>
  <p>Any ideas how to implement this?</p>
  <p>@first_edit 7.07.2021</p>
  <p>I've created this using <a href="https://devops.stackexchange.com/users/12138/samit">Samit's</a> advice:</p>
  <pre><code>http-errors pl_ver
    errorfile 404 /path/to/file
  http-errors en_ver
    errorfile 404 /path/to/file
  
  acl en req.fhdr(accept-language),language(en;pl) -m str en
  acl pl req.fhdr(accept-language),language(en;pl) -m str pl
  
  http-response status 404 errorfiles pl_ver if { status 404 } { pl }
  http-response status 404 errorfiles en_ver if { status 404 } { en }
  </code></pre>
  <p>Anyone have an idea how to implement this in other way?</p>
  <p>@second_edit 24.07.2021</p>
  <p>I've got a better implementation of that problem
  for future seekers here you go:</p>
  <pre><code># Defining HTTP errors
  http-errors pl_ver
    errorfile 404 /path/to/file
  http-errors en_ver
    errorfile 404 /path/to/file
  
  # Setting variable (under frontend section)
    http-request set-var(txn.lang) req.hdr(Accept-Language)
  
  # Feeding with response
    http-response return status 404 errorfiles pl_ver if { status 404 } { var(txn.lang) pl }
    http-response return status 404 errorfiles en_ver if { status 404 } { var(txn.lang) en }
  </code></pre>
  
Tags: <loadbalancing>
Title: How to implement custom HAProxy 404 responses?
UserDisplayName: pete_r
ThreadText: # How to implement custom HAProxy 404 responses?
  
  (Asked by: username_0 on 2021-06-25)
  
  My goal is to build custom 404 http error code that will depend on a user's language. I thought that I can use header accept language.
  
  I thought about something like this:
  
  **#creating 2 acls that accepts only one accept language.**
  
  ```
  acl en req.fhdr(accept-language),language(en;pl) -m str en
  acl pl req.fhdr(accept-language),language(en;pl) -m str pl
  
  ```
  
  **#and here i would like to use backends depends on acl's**
  
  ```
  use_backend polish404 if pl
  use_backend english404 if en
  
  backend polish404
    mode http
    errorfile 503 /path/to/file
  
  backend english404
    mode http
    errorfile 503 /path/to/file
  
  ```
  
  Any ideas how to implement this?
  
  @first\_edit 7.07.2021
  
  I've created this using [username_1's](https://devops.stackexchange.com/users/12138/username_1) advice:
  
  ```
  http-errors pl_ver
    errorfile 404 /path/to/file
  http-errors en_ver
    errorfile 404 /path/to/file
  
  acl en req.fhdr(accept-language),language(en;pl) -m str en
  acl pl req.fhdr(accept-language),language(en;pl) -m str pl
  
  http-response status 404 errorfiles pl_ver if { status 404 } { pl }
  http-response status 404 errorfiles en_ver if { status 404 } { en }
  
  ```
  
  Anyone have an idea how to implement this in other way?
  
  @second\_edit 24.07.2021
  
  I've got a better implementation of that problem
  for future seekers here you go:
  
  ```
  # Defining HTTP errors
  http-errors pl_ver
    errorfile 404 /path/to/file
  http-errors en_ver
    errorfile 404 /path/to/file
  
  # Setting variable (under frontend section)
    http-request set-var(txn.lang) req.hdr(Accept-Language)
  
  # Feeding with response
    http-response return status 404 errorfiles pl_ver if { status 404 } { var(txn.lang) pl }
    http-response return status 404 errorfiles en_ver if { status 404 } { var(txn.lang) en }
  
  ```
  
  **Tags:** loadbalancing
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-25. Score: 0 | Accepted answer
  
  You can make use of http-errors for creating different error code and file mappings.
  
  After that, you can easily make use of `http-response` in haproxy config to return based on the error code. An example is below:
  
  ```
  http-errors site1
      errorfile 404 /etc/haproxy/errors/site1-404.http
  
  http-errors site2
      errorfile 404 /etc/haproxy/errors/site2-404.http
  
  frontend allsites
      bind :80
      default_backend site1-servers
      use_backend site2-servers if { req.hdr(host) site2.com }
  
      # Store host header in variable
      http-request set-var(txn.host) req.hdr(host)
  
      # Use site1 error page if site1.com
      http-response return status 404 errorfiles site1 if { status 404 } { var(txn.host) site1.com }
  
      # Use site2 error page if site2.com
      http-response return status 404 errorfiles site2 if { status 404 } { var(txn.host) site2.com }
  
  ```
  
  You can also dynamically generate different 404 error pages from a single template by passing the request parameters from haproxy to the error template.
  I would recommend to go through this [haproxy blog](https://www.haproxy.com/blog/serve-dynamic-custom-error-pages-with-haproxy/) to understand in detail and try it out.
  
  
  ---
  
  