# 文件驱动的独立模式

在 [文件驱动的独立模式](https://docs.apiseven.com/apisix/production/deployment-modes.md#%E6%96%87%E4%BB%B6%E9%A9%B1%E5%8A%A8) 中，APISIX 从 [`apisix.yaml`](https://docs.apiseven.com/apisix/reference/configuration-files.md#apisixyaml) 或 [`apisix.json`](https://docs.apiseven.com/apisix/reference/configuration-files.md#apisixjson) 获取配置，而不是使用 etcd 作为配置中心。这些配置在启动时加载到内存中，并定期监控更改，无需手动重新加载 APISIX。

important

每个完整的 `apisix.yaml` 都必须以 `#END` 结尾，否则 APISIX 不会加载该文件。

本文档提供了在文件驱动的独立模式下部署的 APISIX 的一些配置示例。要了解有关可用配置选项的更多信息，请参阅 [Admin API 参考](https://docs.apiseven.com/apisix/reference/admin-api.md)（但不要使用这些端点）。这些配置选项可以转换为 YAML 或 JSON，以便在文件驱动的独立模式下使用。

## 配置路由[​](#配置路由 "配置路由的直接链接")

此示例创建了两个路由，将对 `/hello` 和 `/hello2` 的请求转发到不同的上游：

* YAML
* JSON

apisix.yaml

```
routes:

  -

    uri: /hello

    upstream:

      nodes:

        "127.0.0.1:1980": 1

      type: roundrobin

  -

    uri: /hello2

    upstream:

      nodes:

        "127.0.0.1:1981": 1

      type: roundrobin

#END
```

apisix.json

```
{

  "routes": [

    {

      "uri": "/hello",

      "upstream": {

        "nodes": {

          "127.0.0.1:1980": 1

        },

        "type": "roundrobin"

      }

    },

    {

      "uri": "/hello2",

      "upstream": {

        "nodes": {

          "127.0.0.1:1981": 1

        },

        "type": "roundrobin"

      }

    }

  ]

}
```

## 配置路由和服务[​](#配置路由和服务 "配置路由和服务的直接链接")

此示例创建了一个 [服务](https://docs.apiseven.com/apisix/key-concepts/services.md) 和一个服务内的 [路由](https://docs.apiseven.com/apisix/key-concepts/routes.md)：

* YAML
* JSON

apisix.yaml

```
routes:

  -

    uri: /hello

    service_id: 1

services:

  -

    id: 1

    upstream:

      nodes:

        "127.0.0.1:1980": 1

      type: roundrobin

#END
```

apisix.json

```
{

  "routes": [

    {

      "uri": "/hello",

      "service_id": 1

    }

  ],

  "services": [

    {

      "id": 1,

      "upstream": {

        "nodes": {

          "127.0.0.1:1980": 1

        },

        "type": "roundrobin"

      }

    }

  ]

}
```

## 配置路由和上游[​](#配置路由和上游 "配置路由和上游的直接链接")

此示例创建了一个 [上游](https://docs.apiseven.com/apisix/key-concepts/upstreams.md) 和一个指向该上游的 [路由](https://docs.apiseven.com/apisix/key-concepts/routes.md)：

* YAML
* JSON

apisix.yaml

```
routes:

  -

    uri: /hello

    upstream_id: 1

upstreams:

  -

    id: 1

    nodes:

      "127.0.0.1:1980": 1

    type: roundrobin

#END
```

apisix.json

```
{

  "routes": [

    {

      "uri": "/hello",

      "upstream_id": 1

    }

  ],

  "upstreams": [

    {

      "id": 1,

      "nodes": {

        "127.0.0.1:1980": 1

      },

      "type": "roundrobin"

    }

  ]

}
```

## 配置路由、服务和上游[​](#配置路由服务和上游 "配置路由、服务和上游的直接链接")

此示例创建了一个 [上游](https://docs.apiseven.com/apisix/key-concepts/upstreams.md)、一个 [服务](https://docs.apiseven.com/apisix/key-concepts/services.md) 和一个服务内的 [路由](https://docs.apiseven.com/apisix/key-concepts/routes.md)：

* YAML
* JSON

apisix.yaml

```
routes:

  -

    uri: /hello

    service_id: 1

services:

  -

    id: 1

    upstream_id: 2

upstreams:

  -

    id: 2

    nodes:

      "127.0.0.1:1980": 1

    type: roundrobin

#END
```

apisix.json

```
{

  "routes": [

    {

      "uri": "/hello",

      "service_id": 1

    }

  ],

  "services": [

    {

      "id": 1,

      "upstream_id": 2

    }

  ],

  "upstreams": [

    {

      "id": 2,

      "nodes": {

        "127.0.0.1:1980": 1

      },

      "type": "roundrobin"

    }

  ]

}
```

## 配置插件[​](#配置插件 "配置插件的直接链接")

此示例启用了三个 [插件](https://docs.apiseven.com/apisix/key-concepts/plugins.md)：

* YAML
* JSON

apisix.yaml

```
plugins:

  - name: ip-restriction

  - name: jwt-auth

  - name: mqtt-proxy

    stream: true

#END
```

apisix.json

```
{

  "plugins": [

    {

      "name": "ip-restriction"

    },

    {

      "name": "jwt-auth"

    },

    {

      "name": "mqtt-proxy",

      "stream": true

    }

  ]

}
```

对于 L4 插件，将 `stream` 设置为 `true`。

请注意，此配置将覆盖默认启用或在 [配置文件](https://docs.apiseven.com/apisix/reference/configuration-files.md#configyaml-and-configyamlexample)中启用的插件列表。

## 配置插件配置[​](#配置插件配置 "配置插件配置的直接链接")

此示例创建了一个 [插件配置](https://docs.apiseven.com/apisix/key-concepts/plugin-configs.md)，该配置由 [路由](https://docs.apiseven.com/apisix/key-concepts/routes.md) 引用：

* YAML
* JSON

apisix.yaml

```
plugin_configs:

  -

    id: 1

    plugins:

      response-rewrite:

        body: "hello\n"

routes:

  - id: 1

    uri: /hello

    plugin_config_id: 1

    upstream:

      nodes:

        "127.0.0.1:1980": 1

      type: roundrobin

#END
```

apisix.json

```
{

  "plugin_configs": [

    {

      "id": 1,

      "plugins": {

        "response-rewrite": {

          "body": "hello\n"

        }

      }

    }

  ],

  "routes": [

    {

      "id": 1,

      "uri": "/hello",

      "plugin_config_id": 1,

      "upstream": {

        "nodes": {

          "127.0.0.1:1980": 1

        },

        "type": "roundrobin"

      }

    }

  ]

}
```

## 配置全局规则[​](#配置全局规则 "配置全局规则的直接链接")

此示例创建了一个 [全局规则](https://docs.apiseven.com/apisix/key-concepts/plugin-global-rules.md)：

* YAML
* JSON

apisix.yaml

```
global_rules:

  -

    id: 1

    plugins:

      response-rewrite:

        body: "hello\n"

#END
```

apisix.json

```
{

  "global_rules": [

    {

      "id": 1,

      "plugins": {

        "response-rewrite": {

          "body": "hello\n"

        }

      }

    }

  ]

}
```

## 配置插件元数据[​](#配置插件元数据 "配置插件元数据的直接链接")

此示例创建 [插件元数据](https://docs.apiseven.com/apisix/key-concepts/plugin-metadata.md)：

* YAML
* JSON

apisix.yaml

```
upstreams:

  - id: 1

    nodes:

      "127.0.0.1:1980": 1

    type: roundrobin

routes:

  -

    uri: /hello

    upstream_id: 1

    plugins:

      http-logger:

        batch_max_size: 1

        uri: http://127.0.0.1:1980/log

plugin_metadata:

  - id: http-logger

    log_format:

      host: "$host"

      remote_addr: "$remote_addr"

#END
```

apisix.json

```
{

  "upstreams": [

    {

      "id": 1,

      "nodes": {

        "127.0.0.1:1980": 1

      },

      "type": "roundrobin"

    }

  ],

  "routes": [

    {

      "uri": "/hello",

      "upstream_id": 1,

      "plugins": {

        "http-logger": {

          "batch_max_size": 1,

          "uri": "http://127.0.0.1:1980/log"

        }

      }

    }

  ],

  "plugin_metadata": [

    {

      "id": "http-logger",

      "log_format": {

        "host": "$host",

        "remote_addr": "$remote_addr"

      }

    }

  ]

}
```

## 配置 SSL[​](#配置-ssl "配置 SSL的直接链接")

此示例配置 [SSL](https://docs.apiseven.com/apisix/key-concepts/ssl-certificates.md)：

* YAML
* JSON

apisix.yaml

```
ssls:

  -

    cert: $env://SERVER_CERT

    key: $env://SERVER_KEY

    snis:

      - "yourdomain.com"

#END
```

apisix.json

```
{

  "ssls": [

    {

      "cert": "${{SERVER_CERT}}",

      "key": "${{SERVER_KEY}}",

      "snis": [

        "yourdomain.com"

      ]

    }

  ]

}
```

YAML 示例使用环境变量引用，使包含换行符的 PEM 内容在 YAML 文档解析后才解析。请在 `config.yaml` 的 `nginx_config.envs` 中加入 `SERVER_CERT` 和 `SERVER_KEY`，并确保 APISIX 进程可以访问这两个环境变量。详情参阅[将变量传递给 NGINX Worker 进程](https://docs.apiseven.com/apisix/reference/environment-variables.md#%E5%B0%86%E5%8F%98%E9%87%8F%E4%BC%A0%E9%80%92%E7%BB%99-nginx-worker-%E8%BF%9B%E7%A8%8B)。

## 配置消费者[​](#配置消费者 "配置消费者的直接链接")

此示例创建了一个 [消费者](https://docs.apiseven.com/apisix/key-concepts/consumers.md)：

* YAML
* JSON

apisix.yaml

```
consumers:

  - username: jwt

    plugins:

      jwt-auth:

        key: user-key

        secret: my-secret-key

#END
```

apisix.json

```
{

  "consumers": [

    {

      "username": "jwt",

      "plugins": {

        "jwt-auth": {

          "key": "user-key",

          "secret": "my-secret-key"

        }

      }

    }

  ]

}
```

## 配置四层路由[​](#配置四层路由 "配置四层路由的直接链接")

此示例创建了一个 [四层路由](https://docs.apiseven.com/apisix/key-concepts/stream-routes.md)：

* YAML
* JSON

apisix.yaml

```
stream_routes:

  - server_addr: 127.0.0.1

    server_port: 1985

    id: 1

    upstream:

       nodes:

         "127.0.0.1:1981": 1

       type: roundrobin

    plugins:

      mqtt-proxy:

        protocol_name: "MQTT"

        protocol_level: 4

#END
```

apisix.json

```
{

  "stream_routes": [

    {

      "server_addr": "127.0.0.1",

      "server_port": 1985,

      "id": 1,

      "upstream": {

        "nodes": {

          "127.0.0.1:1981": 1

        },

        "type": "roundrobin"

      },

      "plugins": {

        "mqtt-proxy": {

          "protocol_name": "MQTT",

          "protocol_level": 4

        }

      }

    }

  ]

}
```

## 配置 Protocol Buffers[​](#配置-protocol-buffers "配置 Protocol Buffers的直接链接")

此示例创建了一个 [proto 对象](https://docs.apiseven.com/apisix/key-concepts/protos.md)：

* YAML
* JSON

apisix.yaml

```
protos:

  - id: helloworld

    desc: hello world

    content: >

      syntax = "proto3";

      package helloworld;



      service Greeter {

        rpc SayHello (HelloRequest) returns (HelloReply) {}

      }

      message HelloRequest {

        string name = 1;

      }

      message HelloReply {

        string message = 1;

      }

#END
```

apisix.json

```
{

  "protos": [

    {

      "id": "helloworld",

      "desc": "hello world",

      "content": "syntax = \"proto3\";\npackage helloworld;\n\nservice Greeter {\n  rpc SayHello (HelloRequest) returns (HelloReply) {}\n}\nmessage HelloRequest {\n  string name = 1;\n}\nmessage HelloReply {\n  string message = 1;\n}"

    }

  ]

}
```
