Loading

Nginx内置模块简介

经常编译Nginx的时候看到./configure后面跟着很多--with命令,虽然知道是添加模块,但一直也没有仔细去研究这些模块究竟是什么作用。本文会对常用的内置模块做个简单介绍,方便后续检索查看。由于模块之多,不会一一详细介绍,但是会留有参考链接,如感兴趣,可以仔细去研究。

这里建议大家一定要多看官方文档!!!官方文档里的内容才是最全的:包括说明、指令、作用域等等。
官方文档 http://nginx.org/en/docs
中文文档 http://tengine.taobao.org/nginx_docs/cn/docs/

http_auth_basic_module HTTP基本认证

用途:提供HTTP基本认证功能。
内置模块:是。
默认启用:是。如果需要禁用,编译Nginx时使用--without-http_auth_basic_module
作用域:http, server, location, limit_except

示例:

server {
    listen       80;   
    server_name  test.com;

    auth_basic   "登录认证";  
    auth_basic_user_file /etc/nginx-htpasswd;

    root   /mnt/html/www;
    index  index.html;
}

重启Nginx服务后,访问test.com 就会要求输入用户名、密码。

一定要注意auth_basic_user_file路径,如果文件不存在,会不厌其烦的出现403。

参考:
使用crypt配置Basic Auth登录认证 - 飞鸿影~ - 博客园
https://www.cnblogs.com/52fhy/p/9657293.html

http_stub_status_module 状态信息

用途:该模块可以提供 Nginx 的状态信息。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_stub_status_module
作用域:server, location

该模块仅有stub_status这一个指令。

使用示例:

location /nginx_status {
  stub_status on;
  access_log   off;
  allow 127.0.0.1;
  deny all;
}

访问会看到这样的信息:

Active connections: 291
server accepts handled requests
  16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106

其含义:

  • 第一行
    当前的活跃连接数:291
  • 第二行
    服务器已接受的连接数:16630948(accepted connection #)
    服务器已处理的连接数:16630948(handled connection #)
    服务器已处理的请求:31070465(可以算出,平均每个连接有 1.8 个请求)(handled connection #)
  • 第三行
    Reading – Nginx 读取的请求头次数为 6;
    Writting – Nginx 读取请求体、处理请求并发送响应给客户端的次数为 179;
    Waiting – 当前活动的长连接数:106。

参考:
1、解剖Nginx·模块开发篇(5)解读内置非默认模块
https://blog.csdn.net/Poechant/article/details/7627843
2、Module ngx_http_stub_status_module
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html

http_gzip_module 压缩资源

用途:用于支持gzip on等指令,用来减轻服务器的带宽问题,经过gzip压缩后的页面大小可以变为原来的30%甚至更小。
内置模块:是。
默认启用:是。如果需要禁用,编译Nginx时使用--without-http_gzip_module

示例:

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

参考:
1、nginx的gzip压缩功能参数介绍(ngx_http_gzip_module)
https://blog.csdn.net/gnail_oug/article/details/53246026
2、Module ngx_http_gzip_module
http://nginx.org/en/docs/http/ngx_http_gzip_module.html

http_gzip_static_module 支持.gz资源

用途:允许发送以.gz作为文件扩展名的预压缩文件,以替代发送普通文件。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_gzip_static_module

此模块的作用就是在接到请求后,会到url相同的路径的文件系统去找扩展名为.gz的文件,如果存在直接把它发送出去,如果不存在,再将目标文件进行gzip压缩,再发送出去,这样可以避免重复的压缩无谓的消耗资源,这个模块不受gzip_types限制,会对所有请求有效。所以建议不要在全局上使用,因为一般来说大部分都是动态请求,是不会有.gz这个文件的,建议只在局部我们确认有.gz的目录中使用。

该模块仅有gzip_static这一个指令。示例:

gzip_static  on;

参考:
1、Nginx中gzip_static模块的使用介绍 - yancheng的专栏 - CSDN博客
https://blog.csdn.net/yc1022/article/details/21657547
2、Module ngx_http_gzip_static_module
http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html

http_sub_module 字符串替换

用途:该模块用于实现响应内容固定字符串替换。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_sub_module
作用域:http, server, location

示例:

location / {
    sub_filter '<a href="http://127.0.0.1:8080/'  '<a href="https://$host/';
    sub_filter 'nginx.com' 'baidu.com';
    # 是否仅替换一次,如果为off,则全局替换
    sub_filter_once on;
    # 替换的响应类型,*表示替换所有类型
    sub_filter_types text/html;
    # 是否保留原始的Last-Modified。默认是on
    sub_filter_last_modified on;
}

该模块不支持正则替换,灵活性不够。支持正则匹配替换的第三方模块:
1、ngx_http_substitutions_filter_module:https://github.com/yaoweibin/ngx_http_substitutions_filter_module
2、replace-filter-nginx-module:https://github.com/agentzh/replace-filter-nginx-module

参考:
1、nginx ngx_http_sub_module使用 - iuwai - 博客园
https://www.cnblogs.com/iuwai/p/4432084.html
2、nginx的with-http_sub_module模块使用之替换字符串 - 凉生墨客 - 博客园
https://www.cnblogs.com/heruiguo/p/9076239.html
3、nginx使用replace-filter-nginx-module实现内容替换 - 飞鸿影~ - 博客园
https://www.cnblogs.com/52fhy/p/7956099.html

http_addition_module 追加内容

用途:用于在响应之前或者之后追加文本内容,比如想在站点底部追加一个js或者css,可以使用这个模块来实现。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_addition_module

示例:

location / {
        addition_types text/html;
        add_before_body /2013/10/header.html;
        add_after_body  /2013/10/footer.html;
    }

参考:
1、nginx向响应内容中追加内容(ngx_http_addition_module模块) – 运维生存时间
http://www.ttlsa.com/linux/nginx-modules-ngx_http_addition_module/
2、Module ngx_http_addition_module
http://nginx.org/en/docs/http/ngx_http_addition_module.html

http_realip_module 获取实际IP

用途:用于配置REMOTE_ADDR实际IP。 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如,X-Real-IP 或 X-Forwarded-For)。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_realip_module

一般是在客户端和服务端中间增加了代理服务器或者负载均衡,才需要使用这个模块,如果不使用,服务端获取的REMOTE_ADDR就不是客户端的真实IP。

配置:
在后端服务器 location 里头插入

 #指定接收来自哪个前端发送的 IP head 可以是单个IP或者IP段
set_real_ip_from  192.168.1.0/24;
set_real_ip_from 192.168.2.1; 
#IP head  的对应参数,默认即可。
real_ip_header X-Real-IP; 

参考:
1、Module ngx_http_realip_module
http://nginx.org/en/docs/http/ngx_http_realip_module.html
2、--with-http_realip_module选项(后台Nginx服务器记录原始客户端的IP地址 ) - purple尘的专栏 - CSDN博客
https://blog.csdn.net/cscrazybing/article/details/50789234

http_ssl_module 支持HTTPS

用途:此模块为Nginx提供HTTPS支持。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_ssl_module

该模块需要 OpenSSL 库。

yum install openssl openssl-devel

配置示例:

server {                                                                                                                            
    listen      443 ssl;
    listen       80; 
    server_name  52fhy.com www.52fhy.com;
    index index.php index.html index.htm;
    root /www/52fhy.com/;
    
    #ssl on;  #这个开启后导致只能https访问
    ssl_certificate_key  /usr/local/nginx/conf/52fhy.com.key;
    ssl_certificate  /usr/local/nginx/conf/1_52fhy.com_bundle.crt;
    
    if ($scheme = http) {
     rewrite ^(.*)$  https://$host$1 permanent;
    }   
}  

参考:
1、网站使用https协议 - 飞鸿影~ - 博客园
https://www.cnblogs.com/52fhy/p/6139303.html
2、Module ngx_http_ssl_module
http://nginx.org/en/docs/http/ngx_http_ssl_module.html

http_image_filter_module 图片处理

用途:实现图片裁剪、缩放、旋转功能,支持jpg、gif、png格式。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_image_filter_module

依赖GD库:

yum install gd-devel

示例:

按比例裁剪图片:

location ~* .*_(\d+)x(\d+)\.(JPG|jpg|gif|png|PNG)$ {
    set $img_width $1;
    set $img_height $2;
    image_filter   crop  $img_width $img_height;
    image_filter_jpeg_quality  80;
    image_filter_buffer 10M;
    error_page      415  = /empty;
}

可以只指定一个尺寸,另一个尺寸用“-”。如果遇到错误,服务器返回415错误码。

按比例对图像进行缩放:

location ~* .*_(\d+)x(\d+)\.(JPG|jpg|gif|png|PNG)$ {
    set $img_width $1;
    set $img_height $2;
    image_filter   resize  $img_width $img_height;
    image_filter_jpeg_quality  80;
    image_filter_buffer 10M;
    error_page      415  = /empty;
}

参考:
1、Nginx的 http_image_filter_module 模块使用说明 - 学习印记 - CSDN博客
https://blog.csdn.net/revitalizing/article/details/55505853

http_geoip_module 支持GeoIP

用途:GeoIP支持,可以用于IP访问限制。
内置模块:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_geoip_module

参考:
nginx使用GeoIP限制访问并支持白名单 - 阅心笔记
http://www.52os.net/articles/configure-nginx-using-geoip-allow-whitelist.html

http_auth_request_module 第三方auth支持

用途:Nginx默认支持使用auth_basic进行本机验证,也可以使用该模块以支持第三方认证。提供auth_request指令,Nginx 服务器通过 header 的返回状态判断是否认证通过。
内置模块
:是。
默认启用:否。如果需要启用,编译Nginx时使用--with-http_auth_request_module

示例:

server {
    listen 80;
    server_name local.server.com;

    auth_request /auth;

    location / {
        root   html;
        index  index.html;
    }

    location /auth {
        proxy_pass http://auth.server.com/HttpBasicAuthenticate.php;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI $request_uri;
    }
}

参考:
Nginx 的两种认证方式 - WangXiaoQiang - 博客园
http://www.cnblogs.com/wangxiaoqiangs/p/6184181.html

http_flv_module 流媒体点播

一般配合nginx-rtmp-module实现流媒体服务器。相关模块:

  • http_flv_module: 支持flv。内置模块。
  • http_mp4_module: 支持mp4。内置模块。
  • nginx_mod_h264_streaming: 使nginx支持h264编码的视频
  • nginx-rtmp-module: 支持rtmp协议

其中http_flv_modulehttp_mp4_module两个模块是nginx自带的, 可以在编译的时候加上相应的选项。

nginx_mod_h264_streaming的下载地址: http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Nginx-Version2

nginx_mod_h264_streaming 安装:

cd ~
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
$ tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

$ cd ~/nginx-1.7.9
$ ./configure --add-module=$HOME/nginx_mod_h264_streaming-2.2.7 --sbin-path=/usr/local/sbin --with-debug

$ make
$ sudo make install

nginx-rtmp-module托管在GitHub上: https://github.com/arut/nginx-rtmp-module

http_flv_module 配置示例:

location ~ \.flv$ {
    flv;
}

http_mp4_module 配置示例:

location /video/ {
    mp4;
    mp4_buffer_size       1m;
    mp4_max_buffer_size   5m;
    mp4_limit_rate        on;
    mp4_limit_rate_after  30s;
}

参考:
1、Nginx搭建flv视频点播服务器 - wanghetao - 博客园
http://www.cnblogs.com/wanghetao/p/3418744.html
2、nginx实现rtmp,flv,mp4流媒体服务器 - 小雨伞漂流记 - 开源中国
https://my.oschina.net/ososchina/blog/833909
3、从零搭建流媒体服务器+obs推流直播 - qzcsu的博客 - CSDN博客
https://blog.csdn.net/qzcsu/article/details/72782759
4、nginx搭建支持http和rtmp协议的流媒体服务器之一-andersonyan-ChinaUnix博客
http://blog.chinaunix.net/uid-26000296-id-4335063.html
5、nginx搭建支持http和rtmp协议的流媒体服务器之二-andersonyan-ChinaUnix博客
http://blog.chinaunix.net/uid-26000296-id-4335079.html

附录

配置信息

输入./configure --help可以查看Nginx所有支持配置的内置模块的配置信息。其中:

  • with开头的表示该模块默认是未开启的,可以使用--with开启。
  • without开头的表示该模块默认是启用的,可以使用--without禁用。
  • 第三方模块使用--add-module=PATH添加。如果支持动态加载,使用--add-dynamic-module=PATH添加。
$ ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

编译示例

$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-pcre
$ make -j2
$ make install

(完)

posted @ 2019-01-05 22:52  飞鸿影  阅读(6250)  评论(1编辑  收藏  举报