跳转到内容

OpenResty安装

OpenResty 是一个基于 Nginx 的全功能 Web 平台,它打包了标准的 Nginx 核心、许多高质量的 Lua 库、第三方模块以及大多数依赖项。它允许开发者在 Nginx 内部直接运行 Lua 脚本,从而构建极高并发的动态 Web 应用和网关。

推荐使用官方包管理器进行安装以确保稳定性:

Terminal window
# 安装编译与网络依赖
sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl wget gnupg ca-certificates lsb-release
# 导入官方 GPG 密钥
wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/openresty.gpg
# 添加 APT 仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list > /dev/null
# 更新并安装
sudo apt-get update
sudo apt-get -y install openresty

OpenResty 安装后注册为 systemd 服务,使用以下命令管理:

Terminal window
sudo systemctl start openresty # 启动
sudo systemctl restart openresty # 重启
sudo systemctl stop openresty # 停止
sudo systemctl status openresty # 查看状态

建议将站点配置独立出来,保持主配置文件简洁:

/etc/openresty/nginx.conf
http {
# 引入外部配置目录
include /etc/openresty/conf.d/*.conf;
}

理解优先级是排查配置问题的关键:

修饰符匹配类型优先级
=精确匹配1 (最高)
^~最佳前缀匹配(不检查正则)2
~ / ~*正则表达式匹配(区分/不区分大小写)3
(无)普通前缀匹配4

4.3. 复杂正则:环视断言 (Lookaround)

Section titled “4.3. 复杂正则:环视断言 (Lookaround)”

OpenResty 中的正则基于 PCRE,支持强大的环视断言。

  • (?=...) 正向先行断言:后面必须跟着某内容。
  • (?!...) 负向先行断言:后面不能跟着某内容。
  • (?<=...) 正向后行断言:前面必须跟着某内容。
  • (?<!...) 负向后行断言:前面不能跟着某内容。

以下配置实现了“仅允许访问特定后缀文件,其他请求一律拦截”:

server {
listen 80;
server_name blogs.example.com;
root /var/www/blogs;
# 1. 默认处理:支持 SPA 路由
location / {
try_files $uri $uri/ /index.html;
}
# 2. 匹配不包含 "." 的纯路径
location ~* ^\/[a-zA-Z0-9\/_\-]*$ {
try_files $uri $uri/ /index.html;
}
# 3. 安全拦截:拒绝非白名单后缀的访问
# 使用负向后行断言,排除 .md, .html, .ico
location ~* .+(?<!\.md|html|ico)$ {
deny all;
}
}