http_access_phase
该阶段将rewirte和access阶段合为一体进行处理
1、验证client tls
2、设置上下文api_ctx.var : cache & request
3、fetch table
-- always fetch table from the table pool, we don't need a reused api_ctx
local api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
4、route_uri移除末尾的 “/”
api_ctx.var.uri = str_sub(api_ctx.var.uri, 1, #uri - 1)
5、 路径以“/apisix”开头的api被视为内部api,匹配uri
global_rule_skip_internal_api: true # does not run global rule in internal apis
内部uri,不需要执行全局规则
若找不到匹配项:仍然需要执行一次全局规则,直接返回404
6、 设置route、service信息,合并route、service插件 rotue > service
for name, value in pairs(plugin_config.value.plugins) do
route_conf.value.plugins[name] = value
end
#### 拷贝所有的service plugins
local new_conf = core.table.deepcopy(service_conf)
new_conf.value.service_id = new_conf.value.id
new_conf.value.id = route_conf.value.id
new_conf.modifiedIndex = route_conf.modifiedIndex
if route_conf.value.plugins then
for name, conf in pairs(route_conf.value.plugins) do
if not new_conf.value.plugins then
new_conf.value.plugins = {}
end
#### 相同则使用route plugin覆盖
new_conf.value.plugins[name] = conf
end
end
7、 执行一次全局规则
8、处理脚本,若有
if route.value.script then
script.load(route, api_ctx)
script.run("access", api_ctx)
9、调用每个插件中 的"rewrite" 阶段
10、调用每个插件中 的"access" 阶段
11、获取 upstream : id or domain域名
12、服务是否开启websocket,协议是否是grpc
13、设置upstream,和选择服务 (后面细说,有点复杂)
14、执行插件中的before_proxy方法
首先在访问阶段运行before_proxy方法,以避免总是重新初始化请求