# Real IP

The `real-ip` plugin replaces the gateway's client address with an IP address from a request header or query parameter. Use it when a trusted proxy passes a verified client address and the gateway would otherwise identify the proxy as the client.

The plugin is functionally similar to NGINX's [ngx\_http\_realip\_module](https://nginx.org/en/docs/http/ngx_http_realip_module.html) but offers more flexibility.

## Trust Forwarded Client Information[​](#trust-forwarded-client-information "Direct link to Trust Forwarded Client Information")

The gateway can see a client address in more than one place. Each trust setting runs at a different stage and checks a different address.

| Stage                             | Address checked                                                                                              | Effect                                                                                                                                                                             |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| NGINX static real IP settings     | The connection address against `nginx_config.http.real_ip_from`. The default trusts `127.0.0.1` and `unix:`. | For a trusted source, `real_ip_header` reads the configured header (`X-Real-IP` by default), replaces `remote_addr`, and preserves the connection address in `realip_remote_addr`. |
| Global `apisix.trusted_addresses` | `realip_remote_addr`                                                                                         | Retains incoming `Forwarded` and `X-Forwarded-*` information for features such as OpenID Connect redirect URI construction.                                                        |
| Plugin `trusted_addresses`        | The current `remote_addr`, which may already reflect the NGINX rewrite                                       | If the address is trusted, replaces it with the address from `source`.                                                                                                             |

Configure only proxies you operate, and have them overwrite the header you use as the source with a verified client address. When the plugin skips a request, it leaves `remote_addr` at the value it received from NGINX.

## Examples[​](#examples "Direct link to Examples")

The examples set the client address from a query parameter, a trusted proxy header, or a multi-proxy chain.

### Obtain Real Client Address From URI Parameter[​](#obtain-real-client-address-from-uri-parameter "Direct link to Obtain Real Client Address From URI Parameter")

This example reads the client address and port from the `realip` query parameter and returns the resolved values in response headers.

Create a route as follows:

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "real-ip-route",

    "uri": "/get",

    "plugins": {

      "real-ip": {

        "source": "arg_realip",

        "trusted_addresses": ["127.0.0.0/24"]

      },

      "response-rewrite": {

        "headers": {

          "remote_addr": "$remote_addr",

          "remote_port": "$remote_port"

        }

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: httpbin

    labels:

      docs-example: real-ip

    routes:

      - name: real-ip-route

        uris:

          - /get

        plugins:

          real-ip:

            source: arg_realip

            trusted_addresses:

              - 127.0.0.0/24

          response-rewrite:

            headers:

              remote_addr: $remote_addr

              remote_port: $remote_port

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the changes and confirm that they only affect resources for this guide:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

Synchronize the configuration to the gateway:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

* Gateway API
* APISIX CRD

real-ip-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: real-ip-plugin-config

spec:

  plugins:

    - name: real-ip

      config:

        source: arg_realip

        trusted_addresses:

          - 127.0.0.0/24

    - name: response-rewrite

      config:

        headers:

          remote_addr: $remote_addr

          remote_port: $remote_port

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /get

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: real-ip-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

real-ip-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  ingressClassName: apisix

  http:

    - name: real-ip-route

      match:

        paths:

          - /get

      upstreams:

        - name: httpbin-external-domain

      plugins:

        - name: real-ip

          config:

            source: arg_realip

            trusted_addresses:

              - 127.0.0.0/24

        - name: response-rewrite

          config:

            headers:

              remote_addr: $remote_addr

              remote_port: $remote_port
```

Apply the configuration:

```
kubectl apply -f real-ip-ic.yaml
```

❶ Configure `source` to obtain value from the URL parameter `realip` using the [built-in variables](https://docs.api7.ai/apisix/reference/built-in-variables.md).

❷ Use the `response-rewrite` plugin to set response headers to verify if the client IP and port were actually updated.

Send a request to the route with real IP and port in the URL parameter:

```
curl -i "http://127.0.0.1:9080/get?realip=1.2.3.4:9080"
```

You should see the response includes the following headers:

```
remote_addr: 1.2.3.4

remote_port: 9080
```

### Obtain Real Client Address From a Trusted Proxy Header[​](#obtain-real-client-address-from-a-trusted-proxy-header "Direct link to Obtain Real Client Address From a Trusted Proxy Header")

This example configures a route behind a trusted proxy that overwrites `X-Verified-Client-IP` with a verified client address. The gateway uses that value as the client IP and appends it to the `X-Forwarded-For` header sent upstream.

The verification commands use `jq` and assume the test request reaches the gateway from `127.0.0.1` before the plugin runs. If the direct peer is a Docker bridge or another address, add that address or its CIDR to `trusted_addresses`, and use the individual peer address as the expected unchanged value.

Create a route as follows:

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "real-ip-route",

    "uri": "/get",

    "plugins": {

      "real-ip": {

        "source": "http_x_verified_client_ip",

        "trusted_addresses": ["127.0.0.0/24"]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: httpbin

    labels:

      docs-example: real-ip

    routes:

      - name: real-ip-route

        uris:

          - /get

        plugins:

          real-ip:

            source: http_x_verified_client_ip

            trusted_addresses:

              - 127.0.0.0/24

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the changes and confirm that they only affect resources for this guide:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

Synchronize the configuration to the gateway:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

* Gateway API
* APISIX CRD

real-ip-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: real-ip-plugin-config

spec:

  plugins:

    - name: real-ip

      config:

        source: http_x_verified_client_ip

        trusted_addresses:

          - 127.0.0.0/24

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /get

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: real-ip-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

real-ip-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  ingressClassName: apisix

  http:

    - name: real-ip-route

      match:

        paths:

          - /get

      upstreams:

        - name: httpbin-external-domain

      plugins:

        - name: real-ip

          config:

            source: http_x_verified_client_ip

            trusted_addresses:

              - 127.0.0.0/24
```

Apply the configuration:

```
kubectl apply -f real-ip-ic.yaml
```

❶ Configure `source` to read `X-Verified-Client-IP`. The proxy that sends this header must be included in `trusted_addresses`.

Send a request that represents a trusted local proxy supplying the verified client address. Omit `X-Real-IP` and `X-Forwarded-For` so the upstream value reflects this plugin configuration:

```
curl -sS "http://127.0.0.1:9080/get" \

  -H "X-Verified-Client-IP: 198.51.100.20" \

  | jq -r '.headers["X-Forwarded-For"] | split(",")[0]'
```

The first address in the upstream `X-Forwarded-For` header should be the verified client address:

```
198.51.100.20
```

Repeat the request without `X-Verified-Client-IP`:

```
curl -sS "http://127.0.0.1:9080/get" \

  | jq -r '.headers["X-Forwarded-For"] | split(",")[0]'
```

The gateway logs `missing real address`, leaves the client address unchanged, and sends the gateway-visible peer as the first upstream address. Under the assumption above, the command prints `127.0.0.1`.

To verify the trust boundary, change `trusted_addresses` to `["192.0.2.10/32"]`, reapply the configuration with the same interface, and send the first request again. The direct peer is no longer trusted, so the command prints the gateway-visible peer instead of the supplied header value. Restore the trusted address after the test.

### Obtain Real Client Address Behind Multiple Proxies[​](#obtain-real-client-address-behind-multiple-proxies "Direct link to Obtain Real Client Address Behind Multiple Proxies")

This example resolves the original client address from an [`X-Forwarded-For`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) chain added by multiple trusted proxies.

Create a route as follows:

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "id": "real-ip-route",

  "uri": "/get",

  "plugins": {

    "real-ip": {

      "source": "http_x_forwarded_for",

      "recursive": true,

      "trusted_addresses": ["192.128.0.0/16", "127.0.0.1/32"]

    },

    "response-rewrite": {

      "headers": {

        "remote_addr": "$remote_addr"

      }

    }

  },

  "upstream": {

    "type": "roundrobin",

    "nodes": {

      "httpbin.org:80": 1

    }

  }

}'
```

adc.yaml

```
services:

  - name: httpbin

    labels:

      docs-example: real-ip

    routes:

      - name: real-ip-route

        uris:

          - /get

        plugins:

          real-ip:

            source: http_x_forwarded_for

            recursive: true

            trusted_addresses:

              - 192.128.0.0/16

              - 127.0.0.1/32

          response-rewrite:

            headers:

              remote_addr: $remote_addr

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the changes and confirm that they only affect resources for this guide:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

Synchronize the configuration to the gateway:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=real-ip
```

* Gateway API
* APISIX CRD

real-ip-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: real-ip-plugin-config

spec:

  plugins:

    - name: real-ip

      config:

        source: http_x_forwarded_for

        recursive: true

        trusted_addresses:

          - 192.128.0.0/16

          - 127.0.0.1/32

    - name: response-rewrite

      config:

        headers:

          remote_addr: $remote_addr

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /get

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: real-ip-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

real-ip-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: real-ip-route

spec:

  ingressClassName: apisix

  http:

    - name: real-ip-route

      match:

        paths:

          - /get

      upstreams:

        - name: httpbin-external-domain

      plugins:

        - name: real-ip

          config:

            source: http_x_forwarded_for

            recursive: true

            trusted_addresses:

              - 192.128.0.0/16

              - 127.0.0.1/32

        - name: response-rewrite

          config:

            headers:

              remote_addr: $remote_addr
```

Apply the configuration:

```
kubectl apply -f real-ip-ic.yaml
```

❶ Configure `source` to obtain value from the request header `X-Forwarded-For` using the [built-in variables](https://docs.api7.ai/apisix/reference/built-in-variables.md).

❷ Set `recursive` to `true` so the plugin scans the list from right to left, skips addresses in `trusted_addresses`, and selects the first non-trusted address. If every proxy address is trusted, it selects the leftmost address.

❸ Use the `response-rewrite` plugin to set a response header to verify if the client IP was actually updated.

Send a request to the route:

```
curl -i "http://127.0.0.1:9080/get" \

  -H "X-Forwarded-For: 127.0.0.2, 192.128.1.1, 127.0.0.1" 
```

You should see a response including the following header:

```
remote_addr: 127.0.0.2
```
