大网数据广告活动
服务器运维问答

Return a specific/200 status code for a particular URL prefix in Haproxy

· 52CE 我爱测

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 · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · DavidGamba · CC BY-SA 4.0

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)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。