# Detect Upstream Protocol with appProtocol

`appProtocol` is an optional field on a Service port that indicates the type of traffic the backend expects. The Ingress Controller can use this value to automatically determine the upstream protocol.

The example maps one Service's HTTP and HTTPS ports to matching gateway upstream schemes. It assumes the backend Pods are already running; the Service manifest below does not create them.

## Supported appProtocol Values[​](#supported-appprotocol-values "Direct link to Supported appProtocol Values")

The Ingress Controller supports the following `appProtocol` values for Service ports:

| Value               | Explanation                                                                                   |
| ------------------- | --------------------------------------------------------------------------------------------- |
| `http`              | The option sets the upstream scheme to `http`.                                                |
| `https`             | The option sets the upstream scheme to `https`.                                               |
| `kubernetes.io/ws`  | The option sets the upstream scheme to `http` and `enable_websocket` to `true` on the route.  |
| `kubernetes.io/wss` | The option sets the upstream scheme to `https` and `enable_websocket` to `true` on the route. |

When `appProtocol` is not specified, `http` is used as the default protocol.

An HTTPRoute that references an `ExternalName` Service uses the same `appProtocol` detection as an Ingress. Define the referenced Service port and its `appProtocol`; the controller resolves the external DNS name and applies the selected upstream scheme.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Complete [Set Up Ingress Controller and Gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md).
* Provide ready backend Pods in namespace `aic` with the label `app: httpbin`. The Pods must serve HTTP on port `80` and HTTPS on port `443`, matching the Service below. This guide uses an existing backend; creating the Service does not deploy these Pods.

## Kubernetes Service Definition[​](#kubernetes-service-definition "Direct link to Kubernetes Service Definition")

Suppose you want to expose an upstream service `httpbin`. You can define the Service to indicate the expected protocol for each port using `appProtocol`:

httpbin-service.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin

spec:

  selector:

    app: httpbin

  ports:

    - name: http

      port: 80

      targetPort: 80

      appProtocol: http

    - name: https

      port: 443

      targetPort: 443

      appProtocol: https
```

After the backend is running, apply the Service before configuring an HTTPRoute. Verify ready EndpointSlices for that Service; a Service without endpoints does not create a usable HTTPRoute upstream. The protocol is read from the Service port's `appProtocol`, not from an endpoint.

```
kubectl apply -f httpbin-service.yaml

kubectl get endpointslice -n aic -l kubernetes.io/service-name=httpbin
```

## Create a Route[​](#create-a-route "Direct link to Create a Route")

Create a route that references a specific Service port. The Ingress Controller reads the `appProtocol` value associated with the referenced port and uses it to configure the upstream protocol automatically.

In this example, the route will point to the Service port `443`, which was configured with `appProtocol: https`. The upstream scheme will then be automatically set to `https` without any additional configuration.

* Gateway API
* Ingress
* APISIX CRD

httpbin-https.yaml

```
apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: httpbin-route

spec:

  parentRefs:

  - name: apisix

  rules:

  - matches:

    - path:

        type: Exact

        value: /anything

    backendRefs:

    - name: httpbin

      port: 443
```

httpbin-https.yaml

```
apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  namespace: aic

  name: httpbin-route

spec:

  ingressClassName: apisix

  rules:

    - http:

        paths:

          - path: /anything

            pathType: Exact

            backend:

              service:

                name: httpbin

                port:

                  number: 443
```

httpbin-https.yaml

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

kind: ApisixRoute

metadata:

  namespace: aic

  name: httpbin-route

spec:

  ingressClassName: apisix

  http:

  - name: httpbin-route

    match:

      paths:

      - /anything

    backends:

    - serviceName: httpbin

      servicePort: 443
```

Apply the configuration to your cluster:

```
kubectl apply -f httpbin-https.yaml
```

## Verify Upstream Scheme[​](#verify-upstream-scheme "Direct link to Verify Upstream Scheme")

If you are working with API7 Enterprise, you should see in the API7 Dashboard that the upstream scheme of the service has been set to `https`.

If you are working with the APISIX local evaluation from the setup guide, export its configured Admin API key:

```
export ADMIN_API_KEY="$(helm get values -n aic apisix --all -o json | jq -r '.apisix.admin.credentials.admin')"
```

Forward the Admin API's service port to your local machine:

```
kubectl port-forward service/apisix-admin -n aic 9180:9180 &
```

Send a request to `/apisix/admin/configs` to view all configurations synchronized to the standalone gateway:

```
curl "http://127.0.0.1:9180/apisix/admin/configs" -H "X-API-KEY: ${ADMIN_API_KEY}" | jq
```

You should see that the upstream scheme of the service has been set to `https`:

```
{

  "upstreams": [

    {

      "scheme": "https",

      "labels": {

        "managed-by": "apisix-ingress-controller"

      },

      "name": "aic_httpbin-route_0",

      "id": "ee55f349"

    }

  ]

}
```
