跳到主要内容

文件驱动的独立模式

文件驱动的独立模式 中,APISIX 从 apisix.yamlapisix.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: ${{SERVER_CERT}}
key: ${{SERVER_KEY}}
snis:
- "yourdomain.com"
#END

要了解有关设置环境变量的更多信息,请参阅 在配置文件中使用环境变量

配置消费者

此示例创建了一个 消费者

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

配置 Protos

此示例创建了一个 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