Thursday Nov 04, 2010

Reverse proxy and custom content

Reverse proxy content overwrite from sed filters
Recently one of the Web Server customer asked about how to overwrite the content of 404 response code coming from origin-server. Customer tried the following :
Service fn="proxy-retrieve"
Error fn="send-error" code="404" path="/tmp/myfile.txt"

However it didn't work. Why? Because when Service saf proxy-retrieve receives any response from origin server, it send the response back to client. So before send-error Error SAF gets executed, response was already complete. So it didn't work the way customer expected. It works if response code is 504 (Gateway timeout) because for 504 proxy-retrieve SAF doesn't send anything to the client.

Web Server supports powerful sed scripts which came to rescue in this case. What user need to do is to insert a sed filter which does the magic. Fortunately sed filter is also very simple.

Following sed script does that (replaceall.sed):
1,1 {
r /tmp/someotherfile.txt
d
}
2,$ d

$ cat somefile.txt | sed -f replaceall.sed

Above example will replace content of somefile.txt with someotherfile.txt

In Web Server, I tested this and it worked for me.
<If code eq "404">
Output fn="insert-filter" filter="sed-response" sed="1,1 {"
                                                sed="r /tmp/one.txt"
                                                sed="d"
                                                sed="}"
                                                sed="2,\\$ d"
</If>

Above snippet will replace the content of 404 output send by origin server with content of /tmp/one.txt

About

Basant Kukreja

Search

Top Tags
Archives
« November 2010
SunMonTueWedThuFriSat
 
1
2
3
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today