用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
# 6. 编译第三方模块 #### 1. 介绍 nginx是分成一个个模块的,比如core模块,gzip模块(ngx\_http\_gzip\_static\_module),proxy模块(ngx\_http\_proxy\_module),每个模块负责不同的功能,例如ngx\_http\_gzip\_static\_module负责压缩,ngx\_http\_proxy\_module负责反向代理的请求,除了基本的模块,有些模块可以选择编译或不编译进nginx。官网文档中的[Modules reference](http://nginx.org/en/docs/)部分列出了nginx源码包的所有模块。我们可以按照自己的需要定制出一个最适合自己的nginx服务器。假如需要gzip模块,那在编译的时候,可以这样指定。 ``` ./configure --with-http_gzip_static_module ``` 假如不需要fastcgi这个模块,可以这样: ``` ./configure --without-http_fastcgi_module ``` #### 2. 安装 除了源码包提供了各种模块,nginx还有各种各样的第三方模块。官方文档[NGINX 3rd Party Modules](https://www.nginx.com/resources/wiki/modules/)列出了nginx的很多第三方模块,除此之外,很多很有用的模块也能在github等网站上找到。 这些模块提供着各种各样意想不到的功能,有时候我们在语言层面办不好或不好办的事,交给nginx的第三方模块,可能会有惊喜。 我们以这个模块[nginx-module-vts](https://github.com/vozlt/nginx-module-vts)作为例子,来演示一下如果来安装第三方模块和简单的使用。 先把模块的源码下载下来。 ``` $ git clone git://github.com/vozlt/nginx-module-vts.git ``` 配置各种参数,最主要是`--add-module`那一行。 ``` ./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-file-aio \ --with-http_realip_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_fastcgi_module \ --add-module=/home/yinsigan/nginx-module-vts ``` `--add-module`是接刚才下载的模块的绝对路径。 编译安装。 ``` $ make $ sudo make install # 升级可执行文件nginx和重启服务 $ sudo make upgrade ``` 要检测是否成功安装的话,使用`nginx -V`命令即可。 ``` $ nginx -V nginx version: nginx/1.8.0 built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1) built with OpenSSL 1.0.1f 6 Jan 2014 TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --add-module=/home/yinsigan/codes/nginx-module-vts --add-module=/home/yinsigan/codes/nginx-module-url ``` 出现了`nginx-module-vts`,说明安装成功了。 这是添加一种module的情况,假如需要添加很多个module呢,那就再增加一个--add-module就好了。 #### 3. 使用 语法很简单,分别在http和server部分添加几行指令。 ``` http { vhost_traffic_status_zone; ... server { ... location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } } } ``` 运行`sudo nginx -s reload`让配置生效。之后通过浏览器访问`http://127.0.0.1/status`就可以看到效果了。 ![](https://box.kancloud.cn/013cbaca7f93b0921f696dcdf5104d27_957x822.png) 可以看到,这个模块是用来监控nginx的运行情况的,比如反向代理的服务器,cache等情况。 本篇的重点不在于该模块的使用,具体地可以查看官方readme文档,后绪会推出其他模块介绍与使用的文章。 完结。