---
title: "nginx安装第三方模块"
author: "wsluozefeng"
date: "2016-05-26T22:46:59Z"
tags: []
categories: ["操作系统"]
slug: "nginx"
draft: false
lastmod: "2016-05-26T22:46:59Z"
id: 2301199
url: "https://wsluozefeng.iteye.com/blog/2301199"
---

如何安装nginx第三方模块（已经安装nginx和未安装nginx的2种情况）

举例说明：安装第三方的ngx\_cache\_purge模块（用于清除指定URL的缓存），nginx安装目录假设在/usr/local/nginx

下载解压：

wget http://labs.frickle.com/files/ngx\_cache\_purge-2.0.tar.gz

tar -zxvf ngx\_cache\_purge-2.0.tar.gz（目录：/usr/local/src/）

 

进入nginx源码目录（/usr/local/src/nginx/）

cd /usr/local/nginx/

 

 

未安装nginx的情况

大概命令：./configure --prefix=/你的安装目录  **--add-module=/第三方模块目录**  （还可以加上其他需要的编译参数，例如：--with-http\_ssl\_module等）

1.具体命令：

./configure --user=root --group=root --prefix=/usr/local/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module --with-pcre=/usr/local/src/pcre-8.30 --with-zlib=/usr/local/src/zlib-1.2.3 --add-module=/usr/local/src/ngx\_cache\_purge-2.0

2.编译：make

3.安装：make install

 

 

**已安装nginx的情况**

原理：重新编译nginx，但不安装，而是将编译后的nginx（在源码目录下的objs/nginx）覆盖旧的nginx文，重新编译时候，最好保持与旧的编译命令同样的编译参数

 

1.查看安装的nginx编译时候的参数(已安装的模块)：/usr/local/nginx/sbin/nginx -V 

以上命令显示如：configure arguments: --user=root --group=root --prefix=/usr/local/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module --with-pcre=/usr/local/src/pcre-8.30 --with-zlib=/usr/local/src/zlib-1.2.3

 

2.重新编译nginx，并通过--add-module来安装第三方模块

具体命令：

./configure --user=root --group=root --prefix=/usr/local/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module --with-pcre=/usr/local/src/pcre-8.30 --with-zlib=/usr/local/src/zlib-1.2.3 --add-module=/usr/local/src/ngx\_cache\_purge-2.0

 

3.编译：

make

 

4.覆盖旧的nginx文件：(需先停止nginx)

cp objs/nginx /usr/local/nginx/sbin/

 

重启nginx