當來自後端的響應具有狀態404時,首先添加一個Location
標頭,該標頭將原始URI完整地發送到example.com
,然後將狀態碼設置爲302,以便瀏覽器執行重定向。
backend my-backend
mode http
server my-server 203.0.113.113:80 check inter 60000 rise 1 fall 2
http-response set-header Location http://example.com%[capture.req.uri] if { status eq 404 }
http-response set-status 302 if { status eq 404 }
測試:
$ curl -v http://example.org/pics/funny/cat.jpg
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to example.org (127.0.0.1) port 80 (#0)
> GET /pics/funny/cat.jpg HTTP/1.1
> User-Agent: curl/7.35.0
> Host: example.org
> Accept: */*
實際後端返回404,但我們沒有看到它。相反...
< HTTP/1.1 302 Moved Temporarily
< Last-Modified: Thu, 04 Aug 2016 16:59:51 GMT
< Content-Type: text/html
< Content-Length: 332
< Date: Sat, 07 Oct 2017 00:03:22 GMT
< Location: http://example.com/pics/funny/cat.jpg
從後端的404錯誤頁面的響應體將仍然被髮送到瀏覽器,但是 - 事實證明 - 瀏覽器不會顯示出來,所以又何妨呢。這需要HAProxy 1.6或更高版本。