5

In Nginx we can return a specific status code to URL prefix like this.

location /api { return 200; }

How can we achieve the same in Haproxy?.

Gone through Haproxy ACL but couldn't find any.

Augustin
  • 117
  • 2
  • 6
  • The requirement is not clear, returning a code status is a web server feature rather than a reverse proxy one. HAProxy is not a web server. Are you using HAProxy as reverse proxy / load balancer ? and you want to override a backend response ? – Mo3m3n Aug 18 '17 at 12:08
  • yes, want to override the backend response by ignoring the URI prefix or giving a 200 status code – Augustin Aug 18 '17 at 21:00

2 Answers2

3

This can be accomplished by using a lua response script. I already use one to set the CORS headers for the preflight response.

My rule is the following:

global
    ...
    lua-load /etc/haproxy/200.lua

frontend
    bind ...
    ...
    use_backend http_200 if ...

backend http_200
    http-request use-service lua.200-response

Lua script in /etc/haproxy/200.lua:

# 200.lua
core.register_service("200-response", "http", function(applet)
    local response = ""
    applet:set_status(200)
    applet:add_header("Content-Length", "0")
    applet:start_response()
    applet:send(response)
end)
DavidGamba
  • 306
  • 2
  • 8
2

For completeness, from version 2.2 you can return dynamic content like this

http-request return status 200 content-type "text/plain" lf-string "Hello" 

More in docs.

vvucetic
  • 156
  • 4