Refactor documentation for reverse proxy and routing guides
Deploy to Production / deploy (push) Successful in 5s
Deploy to Production / deploy (push) Successful in 5s
This commit is contained in:
+46
-46
@@ -28,12 +28,12 @@
|
||||
<h3>Basic Proxy Configuration</h3>
|
||||
<p>Use the <code>proxy_pass</code> directive in routing:</p>
|
||||
|
||||
<pre><span class="directive">extensions:</span>
|
||||
- <span class="directive">type:</span> <span class="value">routing</span>
|
||||
<span class="directive">config:</span>
|
||||
<span class="directive">regex_locations:</span>
|
||||
<span class="value">"~^/api/"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://localhost:9001"</span></pre>
|
||||
<pre><code class="language-yaml">extensions:
|
||||
- type: routing
|
||||
config:
|
||||
regex_locations:
|
||||
"~^/api/":
|
||||
proxy_pass: "http://localhost:9001"</code></pre>
|
||||
|
||||
<p>All requests to <code>/api/*</code> will be forwarded to <code>http://localhost:9001/api/*</code>.</p>
|
||||
|
||||
@@ -62,21 +62,21 @@
|
||||
<h3>Custom Headers</h3>
|
||||
<p>Add custom headers to proxied requests:</p>
|
||||
|
||||
<pre><span class="value">"~^/api/"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://localhost:9001"</span>
|
||||
<span class="directive">headers:</span>
|
||||
- <span class="value">"X-Custom-Header: my-value"</span>
|
||||
- <span class="value">"Authorization: Bearer token123"</span></pre>
|
||||
<pre><code class="language-bash">"~^/api/":
|
||||
proxy_pass: "http://localhost:9001"
|
||||
headers:
|
||||
- "X-Custom-Header: my-value"
|
||||
- "Authorization: Bearer token123"</code></pre>
|
||||
|
||||
<h3>Dynamic Headers with Captures</h3>
|
||||
<p>Use regex capture groups to build dynamic headers:</p>
|
||||
|
||||
<pre><span class="value">"~^/api/v(?P<version>\\d+)/(?P<service>\\w+)"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://localhost:9001"</span>
|
||||
<span class="directive">headers:</span>
|
||||
- <span class="value">"X-API-Version: {version}"</span>
|
||||
- <span class="value">"X-Service: {service}"</span>
|
||||
- <span class="value">"X-Client-IP: $remote_addr"</span></pre>
|
||||
<pre><code class="language-bash">"~^/api/v(?P<version>\\d+)/(?P<service>\\w+)":
|
||||
proxy_pass: "http://localhost:9001"
|
||||
headers:
|
||||
- "X-API-Version: {version}"
|
||||
- "X-Service: {service}"
|
||||
- "X-Client-IP: $remote_addr"</code></pre>
|
||||
|
||||
<p>Special variables:</p>
|
||||
<ul class="indent">
|
||||
@@ -87,49 +87,49 @@
|
||||
<h3>Proxy Timeout</h3>
|
||||
<p>Configure timeout for proxy requests:</p>
|
||||
|
||||
<pre><span class="comment"># Global default timeout</span>
|
||||
<span class="directive">server:</span>
|
||||
<span class="directive">proxy_timeout:</span> <span class="value">30.0</span>
|
||||
<pre><code class="language-yaml"># Global default timeout
|
||||
server:
|
||||
proxy_timeout: 30.0
|
||||
|
||||
<span class="comment"># Per-route timeout</span>
|
||||
<span class="directive">extensions:</span>
|
||||
- <span class="directive">type:</span> <span class="value">routing</span>
|
||||
<span class="directive">config:</span>
|
||||
<span class="directive">regex_locations:</span>
|
||||
<span class="value">"~^/api/slow"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://localhost:9001"</span>
|
||||
<span class="directive">timeout:</span> <span class="value">120</span> <span class="comment"># 2 minutes for slow endpoints</span></pre>
|
||||
# Per-route timeout
|
||||
extensions:
|
||||
- type: routing
|
||||
config:
|
||||
regex_locations:
|
||||
"~^/api/slow":
|
||||
proxy_pass: "http://localhost:9001"
|
||||
timeout: 120 # 2 minutes for slow endpoints</code></pre>
|
||||
|
||||
<h3>URL Rewriting</h3>
|
||||
<p>The proxy preserves the original request path by default:</p>
|
||||
|
||||
<pre><span class="comment"># Request: GET /api/users/123</span>
|
||||
<span class="comment"># Proxied: GET http://backend:9001/api/users/123</span>
|
||||
<span class="value">"~^/api/"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://backend:9001"</span></pre>
|
||||
<pre><code class="language-bash"># Request: GET /api/users/123
|
||||
# Proxied: GET http://backend:9001/api/users/123
|
||||
"~^/api/":
|
||||
proxy_pass: "http://backend:9001"</code></pre>
|
||||
|
||||
<p>To proxy to a specific path:</p>
|
||||
|
||||
<pre><span class="comment"># Request: GET /api/users/123</span>
|
||||
<span class="comment"># Proxied: GET http://backend:9001/v2/users/123 (path preserved)</span>
|
||||
<span class="value">"~^/api/"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://backend:9001/v2"</span></pre>
|
||||
<pre><code class="language-bash"># Request: GET /api/users/123
|
||||
# Proxied: GET http://backend:9001/v2/users/123 (path preserved)
|
||||
"~^/api/":
|
||||
proxy_pass: "http://backend:9001/v2"</code></pre>
|
||||
|
||||
<h3>Load Balancing Example</h3>
|
||||
<p>Route different services to different backends:</p>
|
||||
|
||||
<pre><span class="directive">extensions:</span>
|
||||
- <span class="directive">type:</span> <span class="value">routing</span>
|
||||
<span class="directive">config:</span>
|
||||
<span class="directive">regex_locations:</span>
|
||||
<span class="value">"~^/api/users"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://user-service:8001"</span>
|
||||
<pre><code class="language-yaml">extensions:
|
||||
- type: routing
|
||||
config:
|
||||
regex_locations:
|
||||
"~^/api/users":
|
||||
proxy_pass: "http://user-service:8001"
|
||||
|
||||
<span class="value">"~^/api/orders"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://order-service:8002"</span>
|
||||
"~^/api/orders":
|
||||
proxy_pass: "http://order-service:8002"
|
||||
|
||||
<span class="value">"~^/api/products"</span>:
|
||||
<span class="directive">proxy_pass:</span> <span class="value">"http://product-service:8003"</span></pre>
|
||||
"~^/api/products":
|
||||
proxy_pass: "http://product-service:8003"</code></pre>
|
||||
|
||||
<h3>Error Handling</h3>
|
||||
<p>pyserve returns appropriate error codes for proxy failures:</p>
|
||||
|
||||
Reference in New Issue
Block a user