跳转至

form-input: NGINX 表单输入模块

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7, 8, 9 和 10
  • CentOS 7, 8, 9
  • AlmaLinux 8, 9
  • Rocky Linux 8, 9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-form-input
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-form-input

通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用模块:

load_module modules/ngx_http_form_input_module.so;

本文档描述了 nginx-module-form-input v0.12,于 2016 年 5 月 16 日发布。


这是一个 nginx 模块,用于读取以 "application/x-www-form-urlencoded" 编码的 HTTP POST 和 PUT 请求体,并将请求体中的参数解析为 nginx 变量。

此模块依赖于 ngx_devel_kit (NDK) 模块。

用法

set_form_input $variable;
set_form_input $variable argument;

set_form_input_multi $variable;
set_form_input_multi $variable argument;

示例:

#nginx.conf

location /foo {
    # 确保 client_max_body_size == client_body_buffer_size
    client_max_body_size 100k;
    client_body_buffer_size 100k;

    set_form_input $data;    # 将 "data" 字段读取到 $data
    set_form_input $foo foo; # 将 "foo" 字段读取到 $foo
}

location /bar {
    # 确保 client_max_body_size == client_body_buffer_size
    client_max_body_size 1m;
    client_body_buffer_size 1m;

    set_form_input_multi $data; # 将所有 "data" 字段读取到 $data
    set_form_input_multi $foo data; # 将所有 "data" 字段读取到 $foo

    array_join ' ' $data; # 现在 $data 是一个字符串
    array_join ' ' $foo;  # 现在 $foo 是一个字符串
}

限制

  • ngx_form_input 将丢弃缓冲到磁盘文件的请求体。当 client_max_body_size 设置大于 client_body_buffer_size 时,超过 client_body_buffer_size(但不超过 client_max_body_size)的请求体将被缓冲到磁盘文件。因此,确保这两个配置设置具有相同的值以避免混淆是很重要的。

GitHub

您可以在 nginx-module-form-input 的 GitHub 仓库 中找到此模块的其他配置提示和文档。