【问题标题】:How to install a module on nginx?如何在 nginx 上安装模块?
【发布时间】:2020-05-16 05:35:24
【问题描述】:

运行 nginx -t 时出现此错误:

nginx: [emerg] unknown directive "subs_filter_types" in /etc/nginx/sites-enabled/my.site.com.conf:285
nginx: configuration file /etc/nginx/nginx.conf test failed

所以我需要安装替换过滤器模块并在 nginx 文档中https://www.nginx.com/resources/wiki/modules/substitutions/#subs-filter-types 其中说运行这些命令:

git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
./configure --add-module=/path/to/module

问题是我的 nginx 安装中的任何地方都没有配置脚本,也没有 git 存储库。我真的不明白。 至少我想知道那个 nginx 配置脚本的内容。

【问题讨论】:

  • 你的意思是sub_filter_types
  • 我不知道。 Subs(替代)是我得到的。这可能是同一件事。
  • 或许不是。我使用subs_filter_types 而不是sub_filter_types 收到相同的错误消息,并且我的Nginx 已经内置了该模块。

标签: nginx module nginx-config


【解决方案1】:

您所指的说明是针对编译安装的。

假设您想将模块添加到您现有的 NGINX 安装中,以下是让事情运行的通用步骤。

  1. 从 nginx.org 获取与您安装的版本完全匹配的 NGINX 版本到您的系统,然后将其解压缩到 /usr/local/src/nginx
  2. git clone NGINX 模块的源代码到你的系统,例如/usr/local/src/nginx-module-foo
  3. cd /usr/local/src/nginx。您可以在此处找到configure 脚本。您基本上将使用相关特定模块的config 的位置配置 NGINX,因此下一步:
  4. ./configure --add-dynamic-module=../nginx-module-foo --with-compat
  5. make

作为编译的补充,您将在 NGINX 源代码的 objs 目录中的某处找到模块的 .so 文件。然后,您将其复制到例如/usr/lib64/nginx/modules/ 目录。

要让您现有的 NGINX 加载模块,请在 /etc/nginx/nginx.conf 的最顶部添加 load_module modules/foo.so;

您可以解读整个编译方法的许多缺点:一个是在生产系统上安装编译软件 (gcc),另一个是在升级 NGINX 或模块时必须重新执行所有这些步骤。

由于上述原因,您可能需要搜索第三方模块的打包安装。

对于 CentOS/RHEL 系统,您可能想查看 GetPageSpeed repos(订阅软件,我偏向于提及它,因为我是维护者。但当时 CentOS/RHEL 8 是免费的写这篇文章。安装你想要的模块,goes down 到几个命令:

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install nginx-module-substitutions

对于基于 Debian 的系统,可能还有其他的 PPA 用于相同的系统。

【讨论】:

  • 获取我的 nginx 版本是什么意思?这是我的版本“nginx 版本:nginx/1.14.0 (Ubuntu)”,我的 /usr/local/src 中没有任何内容。在 /etcnginx 中有一个名为 modules-enabled 的文件夹,那是什么?感谢您的回复。
  • 获取您的 nginx 版本意味着访问 nginx.org 网站,并下载与您当前安装的版本相同的 .tar.gz 文件。例如。在您的情况下,它是this archive。因此,您基本上将wget http://nginx.org/download/nginx-1.14.0.tar.gz 然后tar zxvf ... 然后将其移动到/usr/local/src/nginx(该位置不必存在,我只是为了保持一致性而将其放置)。许多发行版在编译/安装后具有启用模块的不同约定。显然你有那个modules-enabled
  • 对于每个已安装的模块,它很可能具有具有相同 load_module 指令的 .conf 文件。
  • 我想通了。但是现在我在运行脚本时最后收到此错误:(我不想禁用它)
  • 当然,你需要任何依赖库来编译 NGINX。你可能想检查this article
【解决方案2】:

只需将前缀 subs 替换为 sub

对于默认的 nginx 1.10.3 安装(Ubuntu 16.04.5 LTS)

nginx -V 应该有标志 --with-http_sub_module 以使用 sub_* 指令。

使用示例:

sub_filter_types   text/html text/css text/xml;
sub_filter         'needle' 'replacement';
sub_filter_once    off;

NGINX documentation link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多