Skip to main content

a6-plugin-proxy-rewrite

Overview

The proxy-rewrite plugin rewrites request attributes before APISIX forwards the request to the upstream. You can change the URI path, host header, HTTP method, scheme, and add/set/remove request headers — all without modifying your backend service.

When to Use

  • Rewrite the URI path before forwarding (e.g., strip a prefix like /api/v1)
  • Rewrite the Host header for backend routing
  • Change the HTTP method (e.g., convert POST to PUT)
  • Add, set, or remove request headers before proxying
  • Use regex-based URI rewriting for complex path transformations
  • Switch the scheme from HTTP to HTTPS (or vice versa) when proxying

Plugin Configuration Reference (Route/Service)

FieldTypeRequiredDefaultDescription
uristringNoNew upstream request URI. Supports Nginx variables (e.g., $uri, $arg_name).
methodstringNoOverride the HTTP method. Must be uppercase: GET, POST, PUT, DELETE, etc.
hoststringNoNew Host header value sent to upstream.
schemestringNoNew scheme for upstream request: http or https.
headersobjectNoHeader manipulation object with set, add, and remove fields.
headers.setobjectNoSet (overwrite) headers. Key-value pairs. Supports Nginx variables.
headers.addobjectNoAppend headers. Key-value pairs. Adds even if the header already exists.
headers.removearray[string]NoRemove headers. List of header names to strip.
regex_uriarray[string]NoArray of two strings: [pattern, replacement]. Uses PCRE regex to rewrite the URI.
use_real_request_uri_unsafebooleanNofalseUse the original unescaped URI. Security risk — only enable if you understand the implications.

Priority: If both uri and regex_uri are set, uri takes precedence.

Step-by-Step: Enable proxy-rewrite on a Route

1. Simple URI rewrite (strip prefix)

Strip /api/v1 prefix so /api/v1/users becomes /users:

a6 route create -f - <<'EOF'
{
"id": "strip-prefix",
"uri": "/api/v1/*",
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/api/v1/(.*)", "/$1"]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"backend:8080": 1
}
}
}
EOF

2. Rewrite host header

Route to a different virtual host on the backend:

a6 route create -f - <<'EOF'
{
"id": "rewrite-host",
"uri": "/legacy/*",
"plugins": {
"proxy-rewrite": {
"host": "legacy.internal.svc"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"backend:8080": 1
}
}
}
EOF

3. Add and remove headers

a6 route create -f - <<'EOF'
{
"id": "header-manip",
"uri": "/api/*",
"plugins": {
"proxy-rewrite": {
"headers": {
"set": {
"X-Forwarded-Proto": "https",
"X-Real-IP": "$remote_addr"
},
"add": {
"X-Request-Start": "$msec"
},
"remove": ["X-Internal-Debug", "X-Secret-Token"]
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"backend:8080": 1
}
}
}
EOF

Common Patterns

Regex URI rewrite with capture groups

Rewrite /products/123/reviews to /api/products?id=123&section=reviews:

{
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/products/(\\d+)/(.*)$", "/api/products?id=$1&section=$2"]
}
}
}

Change HTTP method

Convert GET to POST for a legacy backend:

{
"plugins": {
"proxy-rewrite": {
"method": "POST"
}
}
}

Static URI replacement

Replace the entire URI path:

{
"plugins": {
"proxy-rewrite": {
"uri": "/internal/health"
}
}
}

Use Nginx variables in URI

{
"plugins": {
"proxy-rewrite": {
"uri": "/api/$arg_version/resource"
}
}
}

Combine URI rewrite with header manipulation

{
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/v2/(.*)", "/v3/$1"],
"headers": {
"set": {
"X-API-Version": "v3"
}
}
}
}
}

Troubleshooting

SymptomCauseFix
URI not rewrittenBoth uri and regex_uri set — uri winsRemove uri if you need regex
Regex not matchingBad pattern or unescaped charactersTest regex with PCRE syntax; escape backslashes in JSON: \\d+
Nginx variable not resolvedVariable name typo or not availableCheck Nginx variable list
404 after rewriteRewritten URI doesn't match upstream pathsVerify the rewritten path exists on the backend
Host header unchangedhost field not set or overridden by upstreamExplicitly set host in proxy-rewrite config
Header appears twiceUsed set vs add confusionUse set to overwrite, add to append

Config Sync Example

version: "1"
routes:
- id: api-rewrite
uri: /api/v1/*
plugins:
proxy-rewrite:
regex_uri:
- "^/api/v1/(.*)"
- "/$1"
headers:
set:
X-Forwarded-Prefix: "/api/v1"
remove:
- X-Debug
upstream_id: backend
upstreams:
- id: backend
type: roundrobin
nodes:
"backend:8080": 1

This page is generated from a6-plugin-proxy-rewrite/SKILL.md in the api7/a6 repository. Browse all skills on the AI Agent Skills page.

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation