-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_with_hooks.yaml
More file actions
245 lines (220 loc) · 7.75 KB
/
Copy pathconfig_with_hooks.yaml
File metadata and controls
245 lines (220 loc) · 7.75 KB
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Example configuration with hook mappings
# This demonstrates all built-in pre-hooks and post-hooks
host: "0.0.0.0"
port: 8080
target_host: "http://example.com"
timeout: 30
log_level: "INFO"
# Hook mappings configuration
hook_mappings:
# ============================================================================
# PRE-HOOKS (execute before proxying, can prevent backend calls)
# ============================================================================
pre_hooks:
# 301 Permanent Redirect - redirect old URLs to new ones
- hostname: "example.com"
url_pattern: "/old-page"
hook: "redirect_301"
params:
location: "https://example.com/new-page"
preserve_query: true
# 301 Redirect with wildcard hostname matching
- hostname: "*.old-domain.com"
url_pattern: "/*"
hook: "redirect_301"
params:
location: "https://new-domain.com/"
preserve_query: true
# 302 Temporary Redirect
- hostname: "example.com"
url_pattern: "/temp-redirect"
hook: "redirect_302"
params:
location: "https://example.com/temporary-page"
# 410 Gone - mark content as permanently removed
- hostname: "example.com"
url_pattern: "/deleted/*"
hook: "gone_410"
params:
message: "This content has been permanently removed"
# 410 Gone for specific paths using regex
- hostname: "api.example.com"
url_pattern: "regex:^/v1/(deprecated|removed)/.*"
hook: "gone_410"
params:
message: "This API endpoint has been deprecated and removed"
# 404 Not Found - return 404 for specific paths
- hostname: "example.com"
url_pattern: "/hidden/*"
hook: "not_found_404"
params:
message: "Page not found"
html: true
# 404 for old API versions
- hostname: "api.example.com"
url_pattern: "/v0/*"
hook: "not_found_404"
params:
message: "API v0 is no longer supported. Please use v2."
# Static HTML - return maintenance page without backend
- hostname: "example.com"
url_pattern: "/maintenance"
hook: "static_html"
params:
html: |
<!DOCTYPE html>
<html>
<head><title>Maintenance Mode</title></head>
<body>
<h1>Site Under Maintenance</h1>
<p>We'll be back soon!</p>
</body>
</html>
status: 503
# Static HTML from file
- hostname: "example.com"
url_pattern: "/custom-page"
hook: "static_html"
params:
file: "/path/to/custom-page.html"
status: 200
# ============================================================================
# POST-HOOKS (execute after receiving response, modify content)
# ============================================================================
post_hooks:
# URL Rewrite - convert REST-style URLs to query parameters
- hostname: "api.example.com"
url_pattern: "/v1/users/*"
hook: "url_rewrite"
params:
pattern: '"/v1/users/([^/"]+)"'
replacement: '"/v1/users?id=$1"'
content_types: ["text/html", "application/json"]
# URL Rewrite - convert multiple path segments
- hostname: "api.example.com"
url_pattern: "/v1/posts/*/comments/*"
hook: "url_rewrite"
params:
pattern: '"/v1/posts/([^/"]+)/comments/([^/"]+)"'
replacement: '"/v1/posts?post_id=$1&comment_id=$2"'
# Text Rewrite - simple find and replace
- hostname: "example.com"
url_pattern: "/blog/*"
hook: "text_rewrite"
params:
pattern: "OldCompanyName"
replacement: "NewCompanyName"
flags: "IGNORECASE"
content_types: ["text/html", "text/plain"]
# Text Rewrite - regex pattern for phone numbers
- hostname: "example.com"
url_pattern: "/contact"
hook: "text_rewrite"
params:
pattern: '\d{3}-\d{3}-\d{4}'
replacement: "XXX-XXX-XXXX"
content_types: ["text/html"]
# Link Rewrite - add .local suffix to all realmo.com links
- hostname: "realmo.com.local"
url_pattern: "/*"
hook: "link_rewrite"
params:
from_domain: "realmo.com"
to_domain: "realmo.com.local"
# Link Rewrite - replace domain in all links
- hostname: "example.com"
url_pattern: "/migrate/*"
hook: "link_rewrite"
params:
from_domain: "old-cdn.com"
to_domain: "new-cdn.com"
attributes: ["href", "src", "action", "data", "poster"]
# HTML Rewrite - change page title
- hostname: "example.com"
url_pattern: "/index.html"
hook: "html_rewrite"
params:
xpath: "//title"
action: "set_text"
value: "Welcome to Our Modified Site"
# HTML Rewrite - set image source attribute
- hostname: "example.com"
url_pattern: "/products/*"
hook: "html_rewrite"
params:
xpath: '//img[@class="logo"]'
action: "set_attr"
attribute: "src"
value: "/new-logo.png"
# HTML Rewrite - remove advertisement divs
- hostname: "example.com"
url_pattern: "/*"
hook: "html_rewrite"
params:
xpath: '//div[@class="advertisement"]'
action: "remove"
# HTML Rewrite - insert analytics script before </head>
- hostname: "example.com"
url_pattern: "/*.html"
hook: "html_rewrite"
params:
xpath: "//head"
action: "insert_before"
value: '<script>console.log("Analytics loaded");</script>'
# HTML Rewrite - insert footer after body
- hostname: "example.com"
url_pattern: "/*"
hook: "html_rewrite"
params:
xpath: "//body"
action: "insert_after"
value: '<div class="footer">Custom Footer</div>'
# XPath Replace from URL - fetch WordPress article and insert into page
- hostname: "example.com"
url_pattern: "/blog/*"
hook: "xpath_replace_from_url"
params:
target_xpath: '//div[@id="article-content"]'
source_url: 'https://wordpress-blog.example.com/wp-json/wp/v2/posts/123'
source_xpath: '//article[@class="post-content"]'
action: "replace_content"
timeout: 10
# XPath Replace from URL - insert external banner
- hostname: "example.com"
url_pattern: "/products/*"
hook: "xpath_replace_from_url"
params:
target_xpath: '//main'
source_url: 'https://api.example.com/banner/promo'
source_xpath: '//div[@class="banner"]'
action: "insert_before"
timeout: 5
# XPath Replace from URL - replace entire element with external content
- hostname: "example.com"
url_pattern: "/news"
hook: "xpath_replace_from_url"
params:
target_xpath: '//section[@id="latest-news"]'
source_url: 'https://news-api.example.com/latest'
source_xpath: '//div[@class="news-feed"]'
action: "replace_element"
# Pattern Matching Examples:
# ============================================================================
#
# Hostname patterns (case-insensitive):
# - "example.com" - exact match
# - "*.example.com" - wildcard subdomain (api.example.com, www.example.com)
# - "*" - matches all hostnames
#
# URL patterns (glob by default):
# - "/api/*" - matches /api/users, /api/posts, etc.
# - "/users/*/profile" - matches /users/123/profile, /users/abc/profile
# - "/*.html" - matches all .html files in root
# - "regex:^/api/v[0-9]+/" - regex pattern (must start with "regex:")
#
# Hook execution order:
# 1. Pre-hooks are checked first (first match wins)
# 2. If pre-hook returns a Response, backend call is skipped
# 3. Post-hooks are all applied in order (all matches are executed)
#
# ============================================================================