- OpenResty Edge Installation FAQ
- OpenResty Edge™ SDK Customer FAQ
- Where to obtain the most critical authentication information (address, port, username, password) when using the SDK?
- My development environment uses a self-signed certificate, and the SDK reports an SSL error when connecting. How to handle it?
- I called the API to modify the configuration, why didn't it take effect? What is the correct release process?
- What are the best practices for automating operations (such as batch updating rules, IP lists) using the SDK?
- How do I integrate the SDK into my CI/CD pipeline (such as Jenkins, GitLab CI) to achieve Configuration as Code?
- OpenResty Edge Dynamic Metrics FAQ
- What is the core value of the "Dynamic Metrics" feature? In what scenarios should I use it?
- Before writing a query, how do I know which data tables and fields are available? Is there a data dictionary?
- I want to count a business metric, such as "count the Top 10 users with the highest request volume based on the user_id field in JWT", is it possible?
- Where are dynamic metrics queries executed? Will they affect the performance of the online gateway?
- OpenResty Edge Edgelang FAQ
- In what scenarios should I use Edgelang instead of the standard page rules UI?
- What is the most powerful application scenario of Edgelang? Can you give an example?
- I have written a piece of Edgelang code, how do I deploy and execute it?
- What is the execution order of Edgelang? How does it cooperate with other rules such as WAF and caching?
- How is the performance and security of Edgelang? What if I write problematic code?
🔗 OpenResty Edge Installation FAQ
🔗 System Requirements and Architecture
🔗 What are the main components of OpenResty Edge and their respective roles?
OpenResty Edge mainly consists of three core components: Edge Admin (management console), Edge Log Server (log and metrics server), and Edge Node (gateway node). Additionally, there are two data storage components: Edge Admin Database and Edge Log Server Database.
🔗 What hardware configuration is required to install OpenResty Edge?
A production environment requires at least three machines, each installing Edge Admin + Admin DB, Log Server + Log Server DB, and Edge Node. For clusters with less than 10 nodes, it is recommended that Edge Admin and Log Server have at least 4 cores, 16GB RAM, and 200GB SSD. The configuration for Edge Node depends on the business volume, generally 1 core with 2GB RAM.
🔗 My network environment has firewall restrictions. Which ports and domains need to be opened?
You need to whitelist port 443 of openresty.com, openresty.org, pkg.openresty.com, and api.openresty.com. Specific ports also need to be opened between components: Edge Admin requires ports 443 and 12345, and Log Server requires ports 12346 and 8089.
🔗 Installation Process
🔗 How to obtain the installation package for OpenResty Edge?
You can obtain the configuration package (openresty-edge-VERSION.tar.gz) for online installation and the complete offline installation package (openresty-edge-bundle-VERSION.tar.gz) from the OpenResty download center (https://openresty.com/en/dashboard/downloads).
🔗 How to get the login account and password for the Edge Admin management interface after installation?
You can use the "Get Default Info" feature of the installer to obtain the default login information, or contact technical support to reset the password.
🔗 How to verify if each component is working properly after installation?
You can check the status of each service using the systemctl status
command, such as sudo systemctl status oredge-admin
. You can also check the log directories of each component, such as /usr/local/oredge-admin/logs
, for any abnormal information.
🔗 Configuration and High Availability
🔗 How to improve the availability of Edge Admin and Log Server?
You can configure two Edge Admin services in dual-master mode or deploy multiple Log Server instances. Edge Admin requires modifying the clone_admin configuration in config.ini, and Edge Node needs to configure the host2 field under the admin section. Multiple Log Server instances need to be added to the configuration of both Edge Admin and Edge Node.
🔗 How to ensure data security? What are the data backup solutions?
It is recommended to regularly back up the database. You can refer to the Database Backup section in the documentation. You can also set up a database cluster to achieve automatic master-slave switching and improve database availability.
🔗 How to update the SSL certificate of Edge Admin after installation?
You can manually replace the /usr/local/oredge-admin/conf/ssl/ssl.crt
and /usr/local/oredge-admin/conf/ssl/ssl.key
files, or specify the certificate path using the -s
and -k
parameters during installation. Alternatively, you can use Edge Node to proxy the traffic of Edge Admin and issue SSL certificate for Edge Admin.
🔗 Troubleshooting
🔗 What to do if the "not yet approved" error appears after installing Edge Node?
This is a normal phenomenon, indicating that Edge Node has successfully connected to Edge Admin and needs to be approved in the Edge Admin management console to join. You can refer to the Gateway Cluster documentation for operation.
🔗 OpenResty Edge™ SDK Customer FAQ
🔗 Where to obtain the most critical authentication information (address, port, username, password) when using the SDK?
This information is consistent with the information you use to log in to the Edge Admin Web console. The parameters required for Edge2Client
initialization are your backend access address and login credentials. If your Admin uses a non-standard HTTPS port, be sure to specify it clearly in the address.
🔗 My development environment uses a self-signed certificate, and the SDK reports an SSL error when connecting. How to handle it?
This is a very common scenario. You only need to execute client.set_ssl_verify(False)
before calling client.login()
to skip certificate verification. However, in a production environment, it is strongly recommended to configure a valid certificate issued by an authoritative CA to ensure security.
🔗 I called the API to modify the configuration, why didn't it take effect? What is the correct release process?
All configuration changes in the SDK are transactional and will not be automatically published. This is a safety design to prevent misoperations from affecting online services. The correct process is "Modify -> Publish -> Verify":
- Modify: Call interfaces such as
new_rule()
,put_app()
to make changes. - Publish: Call
client.new_release()
to create a new version of all pending changes and make it effective. - Verify: (Optional but recommended) Call
client.sync_status()
to check if the configuration has been synchronized to all nodes.
🔗 What are the best practices for automating operations (such as batch updating rules, IP lists) using the SDK?
- Idempotency: Before performing a create operation, first query whether the target exists to avoid duplicate creation. When performing updates, ensure that the script can be run repeatedly.
- Robustness: Use
try...except
to catch exceptions in all API calls and record detailed logs. - Performance considerations: For large-scale batch operations (such as updating thousands of IPs at once), it is recommended to process the data in batches and add a short delay (such as 1 second) between batches to avoid triggering the Admin's protection mechanism due to high request frequency.
🔗 How do I integrate the SDK into my CI/CD pipeline (such as Jenkins, GitLab CI) to achieve Configuration as Code?
This is the most valuable application scenario of the SDK. The usual approach is:
- Code Configuration: Store your Edge application configuration (such as upstreams, rules, etc.) in a specific format (such as YAML, JSON) in the code repository.
- Write a synchronization script: Develop a Python script that reads the coded configuration file and calls the SDK interface to apply the configuration to Edge Admin.
- Integrate into CI/CD: Add a stage in your CI/CD pipeline that automatically triggers the Python script to complete the configuration change and release it to Edge when the configuration code changes.
- Credential Management: Store the high-privilege password for logging into Edge Admin in the CI/CD system's secret management tool and pass it to the script securely to avoid plaintext storage.
🔗 OpenResty Edge Dynamic Metrics FAQ
🔗 What is the core value of the "Dynamic Metrics" feature? In what scenarios should I use it?
Its core value is to provide deep customized business and security insights that standard dashboards cannot satisfy. You should use it in the following scenarios:
- Track specific business metrics: For example, count the number of requests from VIP users, the success rate of API calls for a newly launched event, the distribution of requests from different Android versions, etc.
- Conduct fine-grained security analysis: For example, find the Top 10 IP addresses that attack a specific URI the most, or count the proportion of requests intercepted by WAF from a specific country.
- Implement custom performance monitoring: For example, calculate the P99 response latency of a specific upstream service, rather than just the average.
🔗 Before writing a query, how do I know which data tables and fields are available? Is there a data dictionary?
This is the most critical first step. The data sources you can rely on are mainly the reqs
(request logs) and waf_hits
(WAF logs) virtual tables. To know the exactly available fields they contain (such as client_ip
, status
, uri
, resp_header('Content-Type')
, etc.), the most authoritative way is to consult the Dynamic Metrics section in the OpenResty Edge official documentation.
🔗 I want to count a business metric, such as "count the Top 10 users with the highest request volume based on the user_id
field in JWT", is it possible?
Yes, this is exactly the strength of dynamic metrics. Assuming your user_id
is in the request header named X-Jwt-Claim-User-Id
, the query can be written as follows:
-- Assuming user_id is in the request header 'X-Jwt-Claim-User-Id'
SELECT ngx_var('http_x_jwt_claim_user_id') as user_id, count(*) as count
FROM reqs
WHERE ngx_var('http_x_jwt_claim_user_id') != ''
GROUP BY user_id
ORDER BY count DESC
LIMIT 10;
The key is to use the ngx_var()
function to obtain custom request headers (with the http_
prefix) or other Nginx variables, thereby associating business logic with log data.
🔗 Where are dynamic metrics queries executed? Will they affect the performance of the online gateway?
This is a very important security question. Dynamic metrics queries are executed on the Edge Log Server, which processes log data collected from the gateway nodes in bypass mode. Therefore, no matter how complex your queries are, they will not affect the performance of the Edge Node (gateway node) that handles real-time business traffic. You can safely use this feature for complex data analysis.
🔗 OpenResty Edge Edgelang FAQ
🔗 In what scenarios should I use Edgelang instead of the standard page rules UI?
This is the most core decision issue. You should consider using Edgelang in scenarios where standard UI rules cannot meet the requirements, are complex, dynamic, or have extreme performance requirements.
- Standard Rules UI: Suitable for common, static logic, such as "redirect to
new-url
when URI is/path
". It is simple, intuitive, and ready to use. - Edgelang: Suitable for scenarios that require "programming" logic, such as:
- Complex conditions: Need to make combined judgments based on multiple request headers, Cookies, client IP geographic location, etc.
- Response processing: Need to dynamically modify the response body content (such as inserting JS scripts in HTML) or response headers.
- Dynamic routing: Need to dynamically select upstream services based on specific request parameters (such as user ID, and area code).
- Performance optimization: When you have hundreds of rules, the Edgelang compiler will optimize them into an efficient single code block, which usually performs better than the sequential execution of hundreds of independent page rules.
🔗 What is the most powerful application scenario of Edgelang? Can you give an example?
The most powerful aspect of Edgelang is its deep programming and control capabilities over requests and responses. The most typical "killer" application scenarios are A/B testing and staging releases.
Example: Implement staging release based on user ID Assume you want users with a user ID ending in "7" to access the new upstream service new-backend-upstream
, while other users access the old version.
# Rule: staging Release
# When the cookie in the request contains userid, and userid ends with '7'
req-cookie("userid"), req-cookie("userid") suffix "7" =>
set-upstream-name("new-backend-upstream");
This logic can also be implemented with standard UI rules, but it is more concise and efficient with Edgelang.
🔗 I have written a piece of Edgelang code, how do I deploy and execute it?
Edgelang code is not deployed independently but is executed as an action of a page rule. The process is as follows:
- Write code: Write your Edgelang rules in a code editor.
- Create a page rule: Create a new page rule under an application in Edge Admin.
- Set conditions: (Optional) Set trigger conditions for the requests you want to execute Edgelang, such as "URI path prefix is /api/".
- Select action: In the "action" section of the rule, select "Execute Edgelang" as the action.
- Paste code: Paste your written Edgelang code into the action input box.
- Save and publish: Save and publish this page rule.
After that, when a request matches the conditions you set, your embedded Edgelang code will be executed.
🔗 What is the execution order of Edgelang? How does it cooperate with other rules such as WAF and caching?
As part of the page rules, Edgelang's execution follows the Nginx processing phases and the priority of page rules. A simplified request processing flow is as follows:
- Page rules (including Edgelang or WAF): The request will enter the processing logic of the page rules. At this time, rules containing Edgelang or WAF will be executed according to their priority (which can be dragged and sorted in the UI) and trigger conditions.
- Caching: Edgelang can control subsequent behavior. For example, you can call the
enable-proxy-cache()
action in an Edgelang rule to decide whether to enable caching for a request.
🔗 How is the performance and security of Edgelang? What if I write problematic code?
- Performance: Extremely high. The Edgelang compiler will deeply analyze and optimize your rules (even across multiple rules) to generate very efficient Lua code. Its performance is usually better than the combination of a large number of independent page rules.
- Security:
- Compile-time check: The Edgelang compiler will perform strict syntax and logic checks when you save the rules, and most low-level errors (such as syntax errors, and function name typos) will be discovered and prevented from being saved.
- Sandbox environment: The execution of Edgelang is restricted to a secure sandbox environment, where it can only call the built-in functions and actions listed in the manual, and cannot execute arbitrary system commands or dangerous operations.
- Testing: Although there is currently no independent "dry run" mode, the best practice is to first deploy and test your Edgelang rules on a non-production application or service to verify their logical correctness before applying them to the production environment.
🔗 OpenResty Edge 安装常见问题
🔗 系统要求与架构
🔗 OpenResty Edge 包含哪些主要组件?它们各自的作用是什么?
OpenResty Edge 主要包含三个核心组件:Edge Admin (管理控制台)、Edge Log Server(日志和指标服务器)和 Edge Node(网关节点)。此外还有两个数据存储组件:Edge Admin Database 和 Edge Log Server Database。
🔗 安装 OpenResty Edge 需要什么样的硬件配置?
正式环境至少需要三台机器,分别安装 Edge Admin + Admin DB、Log Server + Log Server DB 和 Edge Node。对于 10 个节点以内的集群,Edge Admin 和 Log Server 推荐至少 4 核 16G 内存 200G SSD,Edge Node 根据业务量而定,一般 1 核心配 2G 内存。
🔗 我的网络环境有防火墙限制,需要开放哪些端口和域名?
需要将 openresty.com 、openresty.org 、pkg.openresty.com 、api.openresty.com 的 443 端口加入白名单。各组件间也需要开放特定端口:Edge Admin 需要开放 443 和 12345 端口,Log Server 需要开放 12346 和 8089 端口。
🔗 安装过程
🔗 如何获取 OpenResty Edge 的安装包?
可以从 OpenResty 的下载中心 (https://openresty.com.cn/cn/dashboard/downloads) 获取在线安装包所需的配置包 (openresty-edge-VERSION.tar.gz) 和完整的离线安装包 (openresty-edge-bundle-VERSION.tar.gz) 。
🔗 安装完成后如何获取 Edge Admin 管理界面的登录账号和密码?
可以使用安装器的“Get Default Info”功能获取默认登录信息,或者联系技术支持重置密码。
🔗 安装完成后如何验证各组件是否正常工作?
可以通过 systemctl status
命令检查各服务状态,例如 sudo systemctl status oredge-admin
,也可以查看各组件的日志目录,如 /usr/local/oredge-admin/logs
中是否有异常信息。
🔗 配置与高可用
🔗 如何提高 Edge Admin 和 Log Server 的可用性?
可以配置两份 Edge Admin 服务为双主模式,或部署多个 Log Server 实例。Edge Admin 需要修改 config.ini 中的 clone_admin 配置,Edge Node 需要配置 admin 段下的 host2 字段。Log Server 多实例需要在 Edge Admin 和 Edge Node 的配置中添加多个 endpoints。
🔗 如何保障数据安全?有哪些数据备份方案?
建议定期备份数据库,可以参考文档中的数据库备份章节。也可以搭建数据库集群,实现主从自动切换,提高数据库可用性。
🔗 安装后如何更新 Edge Admin 的 SSL 证书?
可以手动替换 /usr/local/oredge-admin/conf/ssl/ssl.crt
和 /usr/local/oredge-admin/conf/ssl/ssl.key
文件,或者在安装时通过 -s
和 -k
参数指定证书路径。 另外也可以使用 Edge Node 来代理 Edge Admin 的流量,为 Edge Admin 签发 SSL 证书。
🔗 故障排查
🔗 Edge Node 安装后显示“not yet approved”错误怎么办?
这是正常现象,表示 Edge Node 已成功连接 Edge Admin,需要在 Edge Admin 管理后台中批准该节点加入。可以参考网关集群文档进行操作。
🔗 OpenResty Edge™ SDK 客户常见问题
🔗 使用 SDK 时,最关键的认证信息(地址、端口、用户名、密码)从哪里获取
这些信息与您登录 Edge Admin Web 控制台的信息完全一致。Edge2Client
初始化所需的参数即是您后台的访问地址和登录凭证。如果您的 Admin 使用了非标准的 HTTPS 端口,请务必在地址中明确指定。
🔗 我的开发环境使用自签名证书,SDK 连接时报 SSL 错误,如何处理
这是非常常见的场景。您只需在调用 client.login()
前,执行 client.set_ssl_verify(False)
即可跳过证书验证。但在生产环境,强烈建议配置由权威 CA 签发的有效证书以确保安全。
🔗 我调用 API 修改了配置,为何没生效?正确的发布流程是怎样的
SDK 的所有配置变更都是事务性的,不会自动发布。这是一种安全设计,防止误操作影响线上服务。正确的流程是“修改 -> 发布 -> 验证”:
- 修改:调用
new_rule()
,put_app()
等接口执行变更。 - 发布:调用
client.new_release()
将所有待定变更创建一个新版本并使其生效。 - 验证:(可选但推荐)调用
client.sync_status()
检查配置是否已同步到所有节点。
🔗 使用 SDK 进行自动化操作(如批量更新规则、IP 名单)的最佳实践是什么?
- 幂等性: 在执行创建操作前,先查询目标是否存在,避免重复创建。在执行更新时,确保脚本可重复运行。
- 健壮性:在所有 API 调用外层都使用
try...except
捕获异常,并记录详细日志。 - 性能考量:对于大规模批量操作(如一次更新上千个 IP) ,建议将数据分批处理,并在批次间加入短暂延时(如 1 秒),避免因请求频率过高而触发 Admin 的保护机制。
🔗 如何将 SDK 集成到我的 CI/CD 流水线(如 Jenkins, GitLab CI)中,实现配置即代码(Configuration as Code)?
这是 SDK 最有价值的应用场景。通常的做法是:
- 代码化配置:将您的 Edge 应用配置(如上游、规则等)用特定格式(如 YAML, JSON)存储在代码仓库中。
- 编写同步脚本:开发一个 Python 脚本,该脚本读取代码化的配置文件,并调用 SDK 接口,将配置应用到 Edge Admin 中。
- 集成到 CI/CD:在您的 CI/CD 流水线中增加一个阶段(Stage),当配置代码发生变更时,自动触发该 Python 脚本,完成对 Edge 的配置变更和发布。
- 凭证管理:将登录 Edge Admin 的高权限密码存放在 CI/CD 系统的密钥管理工具中,并通过安全的方式传递给脚本,避免明文存储。
🔗 OpenResty Edge 动态指标常见问题 (FAQ)
🔗 “动态指标”功能的核心价值是什么?什么场景下我应该使用它?
它的核心价值是提供标准仪表盘无法满足的、深度定制的业务和安全洞察力。当您遇到以下场景时,就应该使用它:
- 追踪特定业务指标:例如,统计 VIP 用户的请求数、某个新上线活动的 API 调用成功率、不同安卓版本的请求分布等。
- 进行精细化安全分析:例如,找出攻击某个特定 URI 最多的 Top 10 IP 地址,或统计被 WAF 拦截的请求中,来自某个特定国家的比例。
- 实现自定义的性能监控:例如,计算某个上游服务的 P99 响应延迟,而非仅仅是平均值。
🔗 在写查询前,我如何知道都能用哪些数据表和字段?有数据字典吗?
这是最关键的第一步。您可以依赖的数据源主要是 reqs
(请求日志)和 waf_hits
(WAF 日志)这两个虚拟表。要知道它们包含哪些确切的可用字段(例如 client_ip
, status
, uri
, resp_header('Content-Type')
等),最权威的方式是查阅 OpenResty Edge 官方文档中关于动态指标章节。
🔗 我想统计一个业务指标,比如“根据 JWT 里的 user_id
字段,统计请求量最大的 Top 10 用户”,能做到吗?
可以,这正是动态指标的强大之处。假设您的 user_id
在名为 X-Jwt-Claim-User-Id
的请求头中,查询可以这样写:
-- 假设user_id在请求头'X-Jwt-Claim-User-Id'中
SELECT ngx_var('http_x_jwt_claim_user_id') as user_id, count(*) as count
FROM reqs
WHERE ngx_var('http_x_jwt_claim_user_id') != ''
GROUP BY user_id
ORDER BY count DESC
LIMIT 10;
关键在于利用 ngx_var()
函数来获取自定义请求头(http_
前缀)或其他 Nginx 变量,从而将业务逻辑与日志数据关联起来。
🔗 动态指标查询是在哪里执行的?会不会影响线上网关的性能?
这是一个非常重要的安全问题。动态指标的查询是在 Edge Log Server(日志服务器)上执行的,它处理的是从网关节点旁路采集的日志数据。因此,无论您的查询多复杂,都不会对处理实时业务流量的 Edge Node(网关节点)造成任何性能影响。您可以放心地使用这一功能进行复杂的数据分析。
🔗 OpenResty Edge Edgelang 常见问题
🔗 我应该在什么场景下使用 Edgelang ,而不是使用标准的页面规则 UI?
这是最核心的决策问题。您应该在遇到标准 UI 规则无法满足的、复杂的、动态的或对性能有极致要求的场景时,考虑使用 Edgelang。
- 标准规则 UI: 适用于常见的、静态的逻辑,例如“当 URI 是
/path
时,跳转到new-url
”。它简单、直观、开箱即用。 - Edgelang: 适用于需要“编程”逻辑的场景,例如:
- 条件复杂:需要基于多个请求头、Cookie 、客户端 IP 地理位置等信息进行组合判断。
- 响应处理:需要动态修改响应体内容(比如在 HTML 中插入 JS 脚本)或响应头。
- 动态路由:需要根据请求的特定参数(如用户 ID、区域码)来动态选择上游服务。
- 性能优化:当您有上百条规则时,Edgelang 编译器会将它们优化成高效的单个代码块,其性能通常优于上百条独立页面规则的顺序执行。
🔗 Edgelang 最强大的应用场景是什么?能给个例子吗?
Edgelang 最强大的地方在于其 对请求和响应的深度编程和控制能力。最典型的“杀手级”应用场景是 A/B 测试 和 灰度发布。
示例:基于用户 ID 实现灰度发布 假设您想让用户 ID 尾号为“7”的用户访问新版上游服务 new-backend-upstream
,其他用户访问旧版。
# 规则:灰度发布
# 当请求的 cookie 中包含 userid,且 userid 以 '7' 结尾时
req-cookie("userid"), req-cookie("userid") suffix "7" =>
set-upstream-name("new-backend-upstream");
这个逻辑用标准 UI 规则也可以实现,但用 Edgelang 就更加简洁和高效。
🔗 我写好了一段 Edgelang 代码,如何部署和执行它?
Edgelang 代码并不是独立部署的,而是作为页面规则的一个动作来执行。流程如下:
- 编写代码:在代码编辑器中编写您的 Edgelang 规则。
- 创建页面规则:在 Edge Admin 的某个应用下,创建一个新的页面规则。
- 设置条件:(可选)为您希望执行 Edgelang 的请求设置触发条件,例如
URI路径前缀是 /api/
。 - 选择动作:在规则的“动作”部分,选择“执行 Edgelang”这个动作。
- 粘贴代码:将您写好的 Edgelang 代码粘贴到动作的输入框中。
- 保存并发布:保存并发布这条页面规则。
之后,当有请求匹配您设置的条件时,您嵌入的 Edgelang 代码就会被执行。
🔗 Edgelang 的执行顺序是怎样的?它和 WAF、缓存等其他规则如何协作?
Edgelang 作为页面规则的一部分,其执行遵循 Nginx 的处理阶段(Phases)和页面规则的优先级。一个简化的请求处理流程如下:
- 页面规则(包括 Edgelang 或 WAF):请求将进入页面规则的处理逻辑。此时,包含 Edgelang 或 WAF 的规则会根据其优先级 (在 UI 上可以拖动排序)和触发条件被执行。
- 缓存:Edgelang 可以控制后续的行为。例如,您可以在 Edgelang 规则中调用
enable-proxy-cache()
动作来决定是否对某个请求启用缓存。
🔗 Edgelang 的性能和安全性如何?如果我写了有问题的代码怎么办?
- 性能:极高。Edgelang 编译器会将您的规则(甚至是跨多条规则)进行深度分析和优化,生成非常高效的 Lua 代码。其性能通常优于大量独立页面规则的组合。
- 安全性:
- 编译时检查:Edgelang 编译器会在您保存规则时进行严格的语法和逻辑检查,大部分低级错误(如语法错误、函数名写错)会在此时被发现并阻止保存。
- 沙箱环境:Edgelang 的执行被限制在一个安全的沙箱环境中,它只能调用手册中列出的内置函数和动作,无法执行任意系统命令或危险操作。
- 测试:虽然目前没有独立的“空跑”模式,但最佳实践是在一个非生产的应用或服务上先行部署和测试您的 Edgelang 规则,验证其逻辑正确性后,再应用到生产环境。
🔗 OpenResty Edge 安裝常見問題
🔗 系統要求與架構
🔗 OpenResty Edge 包含哪些主要元件?它們各自的作用是甚麼?
OpenResty Edge 主要包含三個核心元件:Edge Admin (管理控制檯)、Edge Log Server(日誌和指標伺服器)和 Edge Node(閘道器節點)。此外還有兩個資料儲存元件:Edge Admin Database 和 Edge Log Server Database。
🔗 安裝 OpenResty Edge 需要甚麼樣的硬體配置?
正式環境至少需要三臺機器,分別安裝 Edge Admin + Admin DB、Log Server + Log Server DB 和 Edge Node。對於 10 個節點以內的叢集,Edge Admin 和 Log Server 推薦至少 4 核 16G 記憶體 200G SSD,Edge Node 根據業務量而定,一般 1 核心配 2G 記憶體。
🔗 我的網路環境有防火牆限制,需要開放哪些埠和域名?
需要將 openresty.com 、openresty.org 、pkg.openresty.com 、api.openresty.com 的 443 埠加入白名單。各元件間也需要開放特定埠:Edge Admin 需要開放 443 和 12345 埠,Log Server 需要開放 12346 和 8089 埠。
🔗 安裝過程
🔗 如何獲取 OpenResty Edge 的安裝包?
可以從 OpenResty 的下載中心 (https://openresty.com.cn/cn/dashboard/downloads) 獲取線上安裝包所需的配置包 (openresty-edge-VERSION.tar.gz) 和完整的離線安裝包 (openresty-edge-bundle-VERSION.tar.gz) 。
🔗 安裝完成後如何獲取 Edge Admin 管理介面的登入賬號和密碼?
可以使用安裝器的“Get Default Info”功能獲取預設登入資訊,或者聯絡技術支援重置密碼。
🔗 安裝完成後如何驗證各元件是否正常工作?
可以透過 systemctl status
命令檢查各服務狀態,例如 sudo systemctl status oredge-admin
,也可以檢視各元件的日誌目錄,如 /usr/local/oredge-admin/logs
中是否有異常資訊。
🔗 配置與高可用
🔗 如何提高 Edge Admin 和 Log Server 的可用性?
可以配置兩份 Edge Admin 服務為雙主模式,或部署多個 Log Server 例項。Edge Admin 需要修改 config.ini 中的 clone_admin 配置,Edge Node 需要配置 admin 段下的 host2 欄位。Log Server 多例項需要在 Edge Admin 和 Edge Node 的配置中新增多個 endpoints。
🔗 如何保障資料安全?有哪些資料備份方案?
建議定期備份資料庫,可以參考文件中的資料庫備份章節。也可以搭建資料庫叢集,實現主從自動切換,提高資料庫可用性。
🔗 安裝後如何更新 Edge Admin 的 SSL 證書?
可以手動替換 /usr/local/oredge-admin/conf/ssl/ssl.crt
和 /usr/local/oredge-admin/conf/ssl/ssl.key
檔案,或者在安裝時透過 -s
和 -k
引數指定證書路徑。 另外也可以使用 Edge Node 來代理 Edge Admin 的流量,為 Edge Admin 簽發 SSL 證書。
🔗 故障排查
🔗 Edge Node 安裝後顯示“not yet approved”錯誤怎麼辦?
這是正常現象,表示 Edge Node 已成功連線 Edge Admin,需要在 Edge Admin 管理後臺中批准該節點加入。可以參考閘道器叢集文件進行操作。
🔗 OpenResty Edge™ SDK 客戶常見問題
🔗 使用 SDK 時,最關鍵的認證資訊(地址、埠、使用者名稱、密碼)從哪裡獲取
這些資訊與您登入 Edge Admin Web 控制檯的資訊完全一致。Edge2Client
初始化所需的引數即是您後臺的訪問地址和登入憑證。如果您的 Admin 使用了非標準的 HTTPS 埠,請務必在地址中明確指定。
🔗 我的開發環境使用自簽名證書,SDK 連線時報 SSL 錯誤,如何處理
這是非常常見的場景。您只需在呼叫 client.login()
前,執行 client.set_ssl_verify(False)
即可跳過證書驗證。但在生產環境,強烈建議配置由權威 CA 簽發的有效證書以確保安全。
🔗 我呼叫 API 修改了配置,為何沒生效?正確的釋出流程是怎樣的
SDK 的所有配置變更都是事務性的,不會自動釋出。這是一種安全設計,防止誤操作影響線上服務。正確的流程是“修改 -> 釋出 -> 驗證”:
- 修改:呼叫
new_rule()
,put_app()
等介面執行變更。 - 釋出:呼叫
client.new_release()
將所有待定變更建立一個新版本並使其生效。 - 驗證:(可選但推薦)呼叫
client.sync_status()
檢查配置是否已同步到所有節點。
🔗 使用 SDK 進行自動化操作(如批次更新規則、IP 名單)的最佳實踐是甚麼?
- 冪等性: 在執行建立操作前,先查詢目標是否存在,避免重複建立。在執行更新時,確保指令碼可重複執行。
- 健壯性:在所有 API 呼叫外層都使用
try...except
捕獲異常,並記錄詳細日誌。 - 效能考量:對於大規模批次操作(如一次更新上千個 IP) ,建議將資料分批處理,並在批次間加入短暫延時(如 1 秒),避免因請求頻率過高而觸發 Admin 的保護機制。
🔗 如何將 SDK 整合到我的 CI/CD 流水線(如 Jenkins, GitLab CI)中,實現配置即程式碼(Configuration as Code)?
這是 SDK 最有價值的應用場景。通常的做法是:
- 程式碼化配置:將您的 Edge 應用配置(如上游、規則等)用特定格式(如 YAML, JSON)儲存在程式碼倉庫中。
- 編寫同步指令碼:開發一個 Python 指令碼,該指令碼讀取程式碼化的配置檔案,並呼叫 SDK 介面,將配置應用到 Edge Admin 中。
- 整合到 CI/CD:在您的 CI/CD 流水線中增加一個階段(Stage),當配置程式碼發生變更時,自動觸發該 Python 指令碼,完成對 Edge 的配置變更和釋出。
- 憑證管理:將登入 Edge Admin 的高許可權密碼存放在 CI/CD 系統的金鑰管理工具中,並透過安全的方式傳遞給指令碼,避免明文儲存。
🔗 OpenResty Edge 動態指標常見問題 (FAQ)
🔗 “動態指標”功能的核心價值是甚麼?甚麼場景下我應該使用它?
它的核心價值是提供標準儀表盤無法滿足的、深度定製的業務和安全洞察力。當您遇到以下場景時,就應該使用它:
- 追蹤特定業務指標:例如,統計 VIP 使用者的請求數、某個新上線活動的 API 呼叫成功率、不同安卓版本的請求分佈等。
- 進行精細化安全分析:例如,找出攻擊某個特定 URI 最多的 Top 10 IP 地址,或統計被 WAF 攔截的請求中,來自某個特定國家的比例。
- 實現自定義的效能監控:例如,計算某個上游服務的 P99 響應延遲,而非僅僅是平均值。
🔗 在寫查詢前,我如何知道都能用哪些資料表和欄位?有資料字典嗎?
這是最關鍵的第一步。您可以依賴的資料來源主要是 reqs
(請求日誌)和 waf_hits
(WAF 日誌)這兩個虛擬表。要知道它們包含哪些確切的可用欄位(例如 client_ip
, status
, uri
, resp_header('Content-Type')
等),最權威的方式是查閱 OpenResty Edge 官方文件中關於動態指標章節。
🔗 我想統計一個業務指標,比如“根據 JWT 裡的 user_id
欄位,統計請求量最大的 Top 10 使用者”,能做到嗎?
可以,這正是動態指標的強大之處。假設您的 user_id
在名為 X-Jwt-Claim-User-Id
的請求頭中,查詢可以這樣寫:
-- 假設user_id在請求頭'X-Jwt-Claim-User-Id'中
SELECT ngx_var('http_x_jwt_claim_user_id') as user_id, count(*) as count
FROM reqs
WHERE ngx_var('http_x_jwt_claim_user_id') != ''
GROUP BY user_id
ORDER BY count DESC
LIMIT 10;
關鍵在於利用 ngx_var()
函式來獲取自定義請求頭(http_
字首)或其他 Nginx 變數,從而將業務邏輯與日誌資料關聯起來。
🔗 動態指標查詢是在哪裡執行的?會不會影響線上閘道器的效能?
這是一個非常重要的安全問題。動態指標的查詢是在 Edge Log Server(日誌伺服器)上執行的,它處理的是從閘道器節點旁路採集的日誌資料。因此,無論您的查詢多複雜,都不會對處理實時業務流量的 Edge Node(閘道器節點)造成任何效能影響。您可以放心地使用這一功能進行復雜的資料分析。
🔗 OpenResty Edge Edgelang 常見問題
🔗 我應該在甚麼場景下使用 Edgelang ,而不是使用標準的頁面規則 UI?
這是最核心的決策問題。您應該在遇到標準 UI 規則無法滿足的、複雜的、動態的或對效能有極致要求的場景時,考慮使用 Edgelang。
- 標準規則 UI: 適用於常見的、靜態的邏輯,例如“當 URI 是
/path
時,跳轉到new-url
”。它簡單、直觀、開箱即用。 - Edgelang: 適用於需要“程式設計”邏輯的場景,例如:
- 條件複雜:需要基於多個請求頭、Cookie 、客戶端 IP 地理位置等資訊進行組合判斷。
- 響應處理:需要動態修改響應體內容(比如在 HTML 中插入 JS 指令碼)或響應頭。
- 動態路由:需要根據請求的特定引數(如使用者 ID、區域碼)來動態選擇上游服務。
- 效能最佳化:當您有上百條規則時,Edgelang 編譯器會將它們最佳化成高效的單個程式碼塊,其效能通常優於上百條獨立頁面規則的順序執行。
🔗 Edgelang 最強大的應用場景是甚麼?能給個例子嗎?
Edgelang 最強大的地方在於其 對請求和響應的深度程式設計和控制能力。最典型的“殺手級”應用場景是 A/B 測試 和 灰度釋出。
示例:基於使用者 ID 實現灰度釋出 假設您想讓使用者 ID 尾號為“7”的使用者訪問新版上游服務 new-backend-upstream
,其他使用者訪問舊版。
# 規則:灰度釋出
# 當請求的 cookie 中包含 userid,且 userid 以 '7' 結尾時
req-cookie("userid"), req-cookie("userid") suffix "7" =>
set-upstream-name("new-backend-upstream");
這個邏輯用標準 UI 規則也可以實現,但用 Edgelang 就更加簡潔和高效。
🔗 我寫好了一段 Edgelang 程式碼,如何部署和執行它?
Edgelang 程式碼並不是獨立部署的,而是作為頁面規則的一個動作來執行。流程如下:
- 編寫程式碼:在程式碼編輯器中編寫您的 Edgelang 規則。
- 建立頁面規則:在 Edge Admin 的某個應用下,建立一個新的頁面規則。
- 設定條件:(可選)為您希望執行 Edgelang 的請求設定觸發條件,例如
URI路徑字首是 /api/
。 - 選擇動作:在規則的“動作”部分,選擇“執行 Edgelang”這個動作。
- 貼上程式碼:將您寫好的 Edgelang 程式碼貼上到動作的輸入框中。
- 儲存併發布:儲存併發布這條頁面規則。
之後,當有請求匹配您設定的條件時,您嵌入的 Edgelang 程式碼就會被執行。
🔗 Edgelang 的執行順序是怎樣的?它和 WAF、快取等其他規則如何協作?
Edgelang 作為頁面規則的一部分,其執行遵循 Nginx 的處理階段(Phases)和頁面規則的優先順序。一個簡化的請求處理流程如下:
- 頁面規則(包括 Edgelang 或 WAF):請求將進入頁面規則的處理邏輯。此時,包含 Edgelang 或 WAF 的規則會根據其優先順序 (在 UI 上可以拖動排序)和觸發條件被執行。
- 快取:Edgelang 可以控制後續的行為。例如,您可以在 Edgelang 規則中呼叫
enable-proxy-cache()
動作來決定是否對某個請求啟用快取。
🔗 Edgelang 的效能和安全性如何?如果我寫了有問題的程式碼怎麼辦?
- 效能:極高。Edgelang 編譯器會將您的規則(甚至是跨多條規則)進行深度分析和最佳化,生成非常高效的 Lua 程式碼。其效能通常優於大量獨立頁面規則的組合。
- 安全性:
- 編譯時檢查:Edgelang 編譯器會在您儲存規則時進行嚴格的語法和邏輯檢查,大部分低階錯誤(如語法錯誤、函式名寫錯)會在此時被發現並阻止儲存。
- 沙箱環境:Edgelang 的執行被限制在一個安全的沙箱環境中,它只能呼叫手冊中列出的內建函式和動作,無法執行任意系統命令或危險操作。
- 測試:雖然目前沒有獨立的“空跑”模式,但最佳實踐是在一個非生產的應用或服務上先行部署和測試您的 Edgelang 規則,驗證其邏輯正確性後,再應用到生產環境。
- OpenResty Edge インストールに関するよくあるご質問
- OpenResty Edge™ SDK に関するよくあるご質問
- SDK を使用する際、最も重要な認証情報(アドレス、ポート、ユーザー名、パスワード)はどこから取得しますか?
- 開発環境で自己署名証明書を使用しており、SDK 接続時に SSL エラーが発生します。どのように対処しますか?
- API を呼び出して設定を変更しましたが、反映されません。正しいリリースプロセスはどのようなものですか?
- SDK を使用して自動化操作(ルールの一括更新、IP リストなど)を行う際のベストプラクティスは何ですか?
- SDK を CI/CD パイプライン(Jenkins、GitLab CI など)に統合して、コードとしての構成(Configuration as Code)を実現するにはどうすればよいですか?
- OpenResty Edge 動的メトリクスに関するよくあるご質問
- OpenResty Edge Edgelang に関するよくあるご質問
🔗 OpenResty Edge インストールに関するよくあるご質問
🔗 システム要件とアーキテクチャ
🔗 OpenResty Edge にはどのような主要コンポーネントが含まれていますか?それぞれの役割は何ですか?
OpenResty Edge は主に 3 つのコアコンポーネントで構成されています:Edge Admin(管理コンソール)、Edge Log Server(ログおよびメトリクスサーバー)、および Edge Node(ゲートウェイノード)。さらに、2 つのデータストレージコンポーネントがあります:Edge Admin Database と Edge Log Server Database。
🔗 OpenResty Edge をインストールするために必要なハードウェア構成は何ですか?
本番環境には少なくとも 3 台のマシンが必要で、それぞれに Edge Admin + Admin DB、Log Server + Log Server DB、および Edge Node をインストールします。10 ノード以内のクラスターの場合、Edge Admin と Log Server には少なくとも 4 コア、16GB RAM、200GB SSD が推奨されます。Edge Node の構成はトラフィック量に依存し、通常は 1 コアに対して 2GB RAM が目安となります。
🔗 ネットワーク環境にファイアウォールの制限がある場合、どのポートとドメインを開放する必要がありますか?
openresty.com、openresty.org、pkg.openresty.com、api.openresty.com の 443 ポートをホワイトリストに追加する必要があります。コンポーネント間でも特定のポートを開放する必要があります:Edge Admin には 443 および 12345 ポートが必要で、Log Server には 12346 および 8089 ポートが必要です。
🔗 インストールプロセス
🔗 OpenResty Edge のインストールパッケージはどこから入手できますか?
OpenResty のダウンロードセンター (https://openresty.com/en/dashboard/downloads) から、オンラインインストール用の構成パッケージ (openresty-edge-VERSION.tar.gz) と完全なオフラインインストールパッケージ (openresty-edge-bundle-VERSION.tar.gz) を入手できます。
🔗 インストール後、Edge Admin 管理インターフェースのログインアカウントとパスワードをどのように取得しますか?
インストーラーの「Get Default Info」機能を使用してデフォルトのログイン情報を取得するか、テクニカルサポートにご連絡いただきパスワードのリセットを依頼してください。
🔗 インストール後、各コンポーネントが正常に動作しているかどうかを確認するにはどうすればよいですか?
systemctl status
コマンドを使用して各サービスの状態を確認できます。例えば、sudo systemctl status oredge-admin
などです。また、各コンポーネントのログディレクトリ(例:/usr/local/oredge-admin/logs
)に異常情報がないかを確認することもできます。
🔗 設定と高可用性
🔗 Edge Admin と Log Server の可用性を向上させるにはどうすればよいですか?
2 つの Edge Admin サービスをデュアルマスターモードに設定するか、複数の Log Server インスタンスを展開することができます。Edge Admin の場合は config.ini の clone_admin 設定を変更する必要があり、Edge Node の場合は admin セクションの host2 フィールドを設定する必要があります。複数の Log Server インスタンスを使用する場合は、Edge Admin と Edge Node の両方の設定に endpoints を追加する必要があります。
🔗 データの安全性を確保するにはどうすればよいですか?データバックアップのソリューションにはどのようなものがありますか?
定期的にデータベースをバックアップすることをお勧めします。詳細については、ドキュメントのデータベースバックアップセクションをご参照ください。また、データベースクラスターを構築して自動マスター/スレーブ切り替えを実現し、データベースの可用性を向上させることも可能です。
🔗 インストール後、Edge Admin の SSL 証明書を更新するにはどうすればよいですか?
/usr/local/oredge-admin/conf/ssl/ssl.crt
と /usr/local/oredge-admin/conf/ssl/ssl.key
ファイルを手動で置き換えるか、インストール時に -s
および -k
パラメータを使用して証明書パスを指定します。あるいは、Edge Node を使用して Edge Admin のトラフィックをプロキシし、Edge Admin 用の SSL 証明書を発行することも可能です。
🔗 トラブルシューティング
🔗 Edge Node のインストール後に「not yet approved」エラーが表示された場合はどうすればよいですか?
これは正常な現象です。Edge Node が Edge Admin に正常に接続されたことを示しており、Edge Admin 管理コンソールでノードを承認する必要があります。詳細な操作方法についてはゲートウェイクラスターのドキュメントをご参照ください。
🔗 OpenResty Edge™ SDK に関するよくあるご質問
🔗 SDK を使用する際、最も重要な認証情報(アドレス、ポート、ユーザー名、パスワード)はどこから取得しますか?
これらの情報は、Edge Admin Web コンソールにログインする際の情報と完全に一致します。Edge2Client
の初期化に必要なパラメータは、バックエンドのアクセスアドレスとログイン認証情報です。Admin が標準以外の HTTPS ポートを使用している場合は、アドレスにポート番号を明示的に指定してください。
🔗 開発環境で自己署名証明書を使用しており、SDK 接続時に SSL エラーが発生します。どのように対処しますか?
これは非常によくあるケースです。client.login()
を呼び出す前に client.set_ssl_verify(False)
を実行するだけで証明書検証をスキップできます。ただし、本番環境では、信頼できる認証局(CA)によって発行された有効な証明書を設定することを強くお勧めします。
🔗 API を呼び出して設定を変更しましたが、反映されません。正しいリリースプロセスはどのようなものですか?
SDK のすべての設定変更はトランザクション形式であり、自動的に公開されることはありません。これは、誤操作がオンラインサービスに影響を与えるのを防ぐための安全設計です。正しいプロセスは「変更 -> 公開 -> 検証」の流れになります:
- 変更:
new_rule()
,put_app()
などのインターフェースを呼び出して変更を実行します。 - 公開:
client.new_release()
を呼び出して、すべての保留中の変更を新しいバージョンとして作成し、有効にします。 - 検証:(オプションですが推奨)
client.sync_status()
を呼び出して、設定がすべてのノードに同期されているかどうかを確認します。
🔗 SDK を使用して自動化操作(ルールの一括更新、IP リストなど)を行う際のベストプラクティスは何ですか?
- 冪等性:作成操作を実行する前に、対象が既に存在するかどうかを確認し、重複作成を避けます。更新を実行する際は、スクリプトが繰り返し実行できることを確認します。
- 堅牢性:すべての API 呼び出しの外層で
try...except
を使用して例外をキャッチし、詳細なログを記録します。 - パフォーマンス考慮:大規模な一括操作(例:一度に数千の IP を更新する場合)では、データをバッチ処理し、バッチ間に短い遅延(例:1 秒)を追加して、リクエスト頻度が高すぎて Admin の保護機能が作動しないようにすることをお勧めします。
🔗 SDK を CI/CD パイプライン(Jenkins、GitLab CI など)に統合して、コードとしての構成(Configuration as Code)を実現するにはどうすればよいですか?
これは SDK の最も価値のあるアプリケーションシナリオの一つです。一般的なアプローチは次のとおりです:
- コード化された構成:Edge アプリケーションの構成(アップストリーム、ルールなど)を特定の形式(YAML、JSON など)でコードリポジトリに保存します。
- 同期スクリプトの作成:コード化された構成ファイルを読み取り、SDK インターフェースを呼び出して構成を Edge Admin に適用する Python スクリプトを開発します。
- CI/CD への統合:CI/CD パイプラインにステージを追加し、構成コードが変更されたときに自動的に Python スクリプトを実行して、Edge への構成変更とリリースを完了します。
- 認証情報管理:Edge Admin にログインするための高権限パスワードを CI/CD システムのシークレット管理ツールに保存し、スクリプトに安全に渡して、平文での保存を避けます。
🔗 OpenResty Edge 動的メトリクスに関するよくあるご質問
🔗 「動的メトリクス」機能の主な価値は何ですか?どのようなシナリオで使用すべきですか?
その主な価値は、標準のダッシュボードでは得られない、深くカスタマイズされたビジネスおよびセキュリティの洞察を提供することです。次のようなシナリオで使用することをお勧めします:
- 特定のビジネスメトリクスの追跡:例えば、VIP ユーザーのリクエスト数、新規イベントの API 呼び出し成功率、異なる Android バージョンのリクエスト分布などを分析します。
- 詳細なセキュリティ分析:例えば、特定の URI に対して最も多くアクセスする Top 10 の IP アドレスを特定する、または WAF によってブロックされたリクエストのうち特定の国からの割合を集計します。
- カスタムパフォーマンス監視:例えば、特定のアップストリームサービスの P99 応答時間を計算する(単なる平均値ではなく)などが可能です。
🔗 クエリを作成する前に、利用可能なデータテーブルとフィールドを知るにはどうすればよいですか?データディクショナリはありますか?
これは最も重要な最初のステップです。利用できる主なデータソースは reqs
(リクエストログ)と waf_hits
(WAF ログ)という 2 つの仮想テーブルです。これらに含まれる正確な利用可能なフィールド(例:client_ip
、status
、uri
、resp_header('Content-Type')
など)を知るための最も信頼できる方法は、OpenResty Edge 公式ドキュメントの動的メトリクスセクションをご参照ください。
🔗 ビジネスメトリクスを集計したいのですが、例えば「JWT の user_id
フィールドに基づいてリクエスト量が最も多い Top 10 のユーザーを特定する」ことは可能ですか?
はい、これは動的メトリクスの強みです。user_id
が X-Jwt-Claim-User-Id
というリクエストヘッダーにあると仮定すると、クエリは次のように記述できます:
-- user_idがリクエストヘッダー'X-Jwt-Claim-User-Id'にあると仮定
SELECT ngx_var('http_x_jwt_claim_user_id') as user_id, count(*) as count
FROM reqs
WHERE ngx_var('http_x_jwt_claim_user_id') != ''
GROUP BY user_id
ORDER BY count DESC
LIMIT 10;
重要なポイントは、ngx_var()
関数を利用してカスタムリクエストヘッダー(http_
プレフィックス付き)や他の Nginx 変数を取得し、ビジネスロジックとログデータを関連付けることです。
🔗 動的メトリクスのクエリはどこで実行されますか?オンラインゲートウェイのパフォーマンスに影響しますか?
これは非常に重要なセキュリティに関する質問です。動的メトリクスのクエリは、Edge Log Server(ログサーバー)上で実行され、ゲートウェイノードから副次的に収集されたログデータを処理します。そのため、クエリがどれだけ複雑であっても、リアルタイムのビジネストラフィックを処理する Edge Node(ゲートウェイノード)のパフォーマンスには影響を与えません。この機能を安心して使用し、複雑なデータ分析を行うことができます。
🔗 OpenResty Edge Edgelang に関するよくあるご質問
🔗 標準のページルール UI ではなく、どのようなシナリオで Edgelang を使用すべきですか?
これは最も重要な判断ポイントです。標準の UI ルールでは対応できない、複雑で動的、または極めて高いパフォーマンスが要求されるシナリオで Edgelang の使用を検討すべきです。
- 標準ルール UI:一般的で静的なロジックに適しています。例えば、「URI が
/path
の場合にnew-url
にリダイレクトする」など。これはシンプルで直感的、すぐに使用可能です。 - Edgelang:「プログラミング」ロジックが必要なシナリオに適しています。例えば:
- 複雑な条件:複数のリクエストヘッダー、Cookie、クライアント IP の地理的位置などに基づいて複合的な判断が必要な場合。
- レスポンス処理:レスポンス本文の内容を動的に変更する必要がある場合(例えば、HTML に JavaScript を挿入する)やレスポンスヘッダーを変更する必要がある場合。
- 動的ルーティング:特定のリクエストパラメータ(ユーザー ID や地域コードなど)に基づいてアップストリームサービスを動的に選択する必要がある場合。
- パフォーマンス最適化:数百のルールがある場合、Edgelang コンパイラはそれらを効率的な単一のコードブロックに最適化し、通常は数百の独立したページルールを順次実行するよりも優れたパフォーマンスを発揮します。
🔗 Edgelang の最も強力な応用シナリオは何ですか?例を挙げていただけますか?
Edgelang の最も強力な特長は、リクエストとレスポンスに対する高度なプログラミングと制御能力です。最も代表的な「キラー」アプリケーションシナリオは、A/B テストとカナリアリリース(段階的リリース)です。
例:ユーザー ID に基づくカナリアリリースの実装 ユーザー ID が「7」で終わるユーザーに新しいアップストリームサービス new-backend-upstream
へのアクセスを許可し、他のユーザーには旧バージョンを提供したいとします。
# ルール:カナリアリリース
# リクエストのCookieにuseridが含まれ、useridが'7'で終わる場合
req-cookie("userid"), req-cookie("userid") suffix "7" =>
set-upstream-name("new-backend-upstream");
このロジックは標準の UI ルールでも実装可能ですが、Edgelang を使用するとより簡潔で効率的になります。
🔗 Edgelang コードを作成した後、どのようにデプロイして実行しますか?
Edgelang コードは独立してデプロイされるのではなく、ページルールのアクションとして実行されます。プロセスは次のとおりです:
- コードの作成:コードエディタで Edgelang ルールを作成します。
- ページルールの作成:Edge Admin のアプリケーション下で新しいページルールを作成します。
- 条件の設定:(オプション)Edgelang を実行したいリクエストのトリガー条件を設定します。例えば、「URI パスのプレフィックスが /api/ である」など。
- アクションの選択:ルールの「アクション」セクションで「Edgelang を実行」を選択します。
- コードの貼り付け:作成した Edgelang コードをアクション入力ボックスに貼り付けます。
- 保存と公開:このページルールを保存して公開します。
その後、設定した条件に一致するリクエストがあると、埋め込まれた Edgelang コードが実行されます。
🔗 Edgelang の実行順序はどのようになっていますか?WAF やキャッシュなどの他のルールとどのように連携しますか?
ページルールの一部として、Edgelang の実行は Nginx の処理フェーズとページルールの優先順位に従います。簡略化されたリクエスト処理フローは次のとおりです:
- ページルール(Edgelang または WAF を含む):リクエストはページルールの処理ロジックに入ります。この時点で、Edgelang または WAF を含むルールは、その優先順位(UI でドラッグして並べ替え可能)とトリガー条件に従って実行されます。
- キャッシュ:Edgelang は後続の動作を制御できます。例えば、Edgelang ルールで
enable-proxy-cache()
アクションを呼び出して、特定のリクエストに対してキャッシュを有効にするかどうかを決定できます。
🔗 Edgelang のパフォーマンスとセキュリティはどのようになっていますか?問題のあるコードを書いた場合はどうすればよいですか?
- パフォーマンス:非常に高性能です。Edgelang コンパイラは、ルール(複数のルールにまたがる場合でも)を深く分析して最適化し、非常に効率的な Lua コードを生成します。そのパフォーマンスは通常、多数の独立したページルールの組み合わせよりも優れています。
- セキュリティ:
- コンパイル時チェック:Edgelang コンパイラは、ルールを保存する際に厳格な構文とロジックのチェックを行い、ほとんどの低レベルエラー(構文エラーや関数名の誤字など)はこの段階で検出され、保存が阻止されます。
- サンドボックス環境:Edgelang の実行は安全なサンドボックス環境に制限されており、マニュアルに記載されている組み込み関数とアクションのみを呼び出すことができ、任意のシステムコマンドや危険な操作を実行することはできません。
- テスト:現在、独立した「ドライラン」モードはありませんが、ベストプラクティスとして、非本番環境のアプリケーションやサービスで最初に Edgelang ルールをデプロイしてテストし、その論理的な正確性を検証してから本番環境に適用することをお勧めします。