跳到主要内容
版本:3.20.0

文件驱动的独立模式

在 文件驱动的独立模式 中,APISIX 从 apisix.yaml 或 apisix.json 获取配置,而不是使用 etcd 作为配置中心。这些配置在启动时加载到内存中,并定期监控更改,无需手动重新加载 APISIX。

important

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

本文档提供了在文件驱动的独立模式下部署的 APISIX 的一些配置示例。要了解有关可用配置选项的更多信息,请参阅 Admin API 参考(但不要使用这些端点)。这些配置选项可以转换为 YAML 或 JSON,以便在文件驱动的独立模式下使用。

配置路由​

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

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.yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END

配置路由和上游​

此示例创建了一个 上游 和一个指向该上游的 路由:

apisix.yaml
routes:
-
uri: /hello
upstream_id: 1
upstreams:
-
id: 1
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END

配置路由、服务和上游​

此示例创建了一个 上游、一个 服务 和一个服务内的 路由:

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.yaml
plugins:
- name: ip-restriction
- name: jwt-auth
- name: mqtt-proxy
stream: true
#END

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

请注意,此配置将覆盖默认启用或在 配置文件中启用的插件列表。

配置插件配置​

此示例创建了一个 插件配置,该配置由 路由 引用:

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.yaml
global_rules:
-
id: 1
plugins:
response-rewrite:
body: "hello\n"
#END

配置插件元数据​

此示例创建 插件元数据:

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

配置 SSL​

此示例配置 SSL:

apisix.yaml
ssls:
-
cert: $env://SERVER_CERT
key: $env://SERVER_KEY
snis:
- "yourdomain.com"
#END

YAML 示例使用环境变量引用,使包含换行符的 PEM 内容在 YAML 文档解析后才解析。请在 config.yaml 的 nginx_config.envs 中加入 SERVER_CERT 和 SERVER_KEY,并确保 APISIX 进程可以访问这两个环境变量。详情参阅将变量传递给 NGINX Worker 进程。

配置消费者​

此示例创建了一个 消费者:

apisix.yaml
consumers:
- username: jwt
plugins:
jwt-auth:
key: user-key
secret: my-secret-key
#END

配置四层路由​

此示例创建了一个 四层路由:

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

配置 Protocol Buffers​

此示例创建了一个 proto 对象:

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