常常编译Nginx的时候看到./configure
后面跟着不少--with
命令,虽然知道是添加模块,但一直也没有仔细去研究这些模块到底是什么做用。本文会对经常使用的内置模块作个简单介绍,方便后续检索查看。因为模块之多,不会一一详细介绍,可是会留有参考连接,如感兴趣,能够仔细去研究。javascript
这里建议你们必定要多看官方文档!!!官方文档里的内容才是最全的:包括说明、指令、做用域等等。
官方文档 http://nginx.org/en/docs
中文文档 http://tengine.taobao.org/nginx_docs/cn/docs/php
用途:提供HTTP基本认证功能。
内置模块:是。
默认启用:是。若是须要禁用,编译Nginx时使用--without-http_auth_basic_module
。
做用域:http, server, location, limit_exceptcss
示例:html
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。java
参考:
使用crypt配置Basic Auth登陆认证 - 飞鸿影~ - 博客园
http://www.javashuo.com/article/p-hmjvyllc-da.htmllinux
用途:该模块能够提供 Nginx
的状态信息。
内置模块:是。
默认启用:否。若是须要启用,编译Nginx时使用--with-http_stub_status_module
。
做用域:server, locationnginx
该模块仅有stub_status
这一个指令。git
使用示例:github
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
其含义:
参考:
一、解剖Nginx·模块开发篇(5)解读内置非默认模块
https://blog.csdn.net/Poechant/article/details/7627843
二、Module ngx_http_stub_status_module
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
用途:用于支持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]\.";
参考:
一、nginx的gzip压缩功能参数介绍(ngx_http_gzip_module)
https://blog.csdn.net/gnail_oug/article/details/53246026
二、Module ngx_http_gzip_module
http://nginx.org/en/docs/http/ngx_http_gzip_module.html
用途:容许发送以.gz
做为文件扩展名的预压缩文件,以替代发送普通文件。
内置模块:是。
默认启用:否。若是须要启用,编译Nginx时使用--with-http_gzip_static_module
。
此模块的做用就是在接到请求后,会到url相同的路径的文件系统去找扩展名为
.gz
的文件,若是存在直接把它发送出去,若是不存在,再将目标文件进行gzip压缩,再发送出去,这样能够避免重复的压缩无谓的消耗资源,这个模块不受gzip_types
限制,会对全部请求有效。因此建议不要在全局上使用,由于通常来讲大部分都是动态请求,是不会有.gz
这个文件的,建议只在局部咱们确认有.gz
的目录中使用。
该模块仅有gzip_static这一个指令。示例:
gzip_static on;
参考:
一、Nginx中gzip_static模块的使用介绍 - yancheng的专栏 - CSDN博客
https://blog.csdn.net/yc1022/article/details/21657547
二、Module ngx_http_gzip_static_module
http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
用途:该模块用于实现响应内容固定字符串替换。
内置模块:是。
默认启用:否。若是须要启用,编译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; }
该模块不支持正则替换,灵活性不够。支持正则匹配替换的第三方模块:
一、ngx_http_substitutions_filter_module:https://github.com/yaoweibin/ngx_http_substitutions_filter_module
二、replace-filter-nginx-module:https://github.com/agentzh/replace-filter-nginx-module
参考:
一、nginx ngx_http_sub_module使用 - iuwai - 博客园
http://www.javashuo.com/article/p-fgpmcfdg-dm.html
二、nginx的with-http_sub_module模块使用之替换字符串 - 凉生墨客 - 博客园
http://www.javashuo.com/article/p-tnksqdlx-du.html
三、nginx使用replace-filter-nginx-module实现内容替换 - 飞鸿影~ - 博客园
http://www.javashuo.com/article/p-qddanbto-ec.html
用途:用于在响应以前或者以后追加文本内容,好比想在站点底部追加一个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; }
参考:
一、nginx向响应内容中追加内容(ngx_http_addition_module模块) – 运维生存时间
http://www.ttlsa.com/linux/nginx-modules-ngx_http_addition_module/
二、Module ngx_http_addition_module
http://nginx.org/en/docs/http/ngx_http_addition_module.html
用途:用于配置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;
参考:
一、Module ngx_http_realip_module
http://nginx.org/en/docs/http/ngx_http_realip_module.html
二、--with-http_realip_module选项(后台Nginx服务器记录原始客户端的IP地址 ) - purple尘的专栏 - CSDN博客
https://blog.csdn.net/cscrazybing/article/details/50789234
用途:此模块为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; } }
参考:
一、网站使用https协议 - 飞鸿影~ - 博客园
http://www.javashuo.com/article/p-opzzxbwm-er.html
二、Module ngx_http_ssl_module
http://nginx.org/en/docs/http/ngx_http_ssl_module.html
用途:实现图片裁剪、缩放、旋转功能,支持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; }
参考:
一、Nginx的 http_image_filter_module 模块使用说明 - 学习印记 - CSDN博客
https://blog.csdn.net/revitalizing/article/details/55505853
用途:GeoIP支持,能够用于IP访问限制。
内置模块:是。
默认启用:否。若是须要启用,编译Nginx时使用--with-http_geoip_module
。
参考:
nginx使用GeoIP限制访问并支持白名单 - 阅心笔记
http://www.52os.net/articles/configure-nginx-using-geoip-allow-whitelist.html
用途: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
通常配合nginx-rtmp-module实现流媒体服务器。相关模块:
其中http_flv_module
和http_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; }
参考:
一、Nginx搭建flv视频点播服务器 - wanghetao - 博客园
http://www.cnblogs.com/wanghetao/p/3418744.html
二、nginx实现rtmp,flv,mp4流媒体服务器 - 小雨伞漂流记 - 开源中国
https://my.oschina.net/ososchina/blog/833909
三、从零搭建流媒体服务器+obs推流直播 - qzcsu的博客 - CSDN博客
http://www.javashuo.com/article/p-exwscgce-dt.html
四、nginx搭建支持http和rtmp协议的流媒体服务器之一-andersonyan-ChinaUnix博客
http://blog.chinaunix.net/uid-26000296-id-4335063.html
五、nginx搭建支持http和rtmp协议的流媒体服务器之二-andersonyan-ChinaUnix博客
http://blog.chinaunix.net/uid-26000296-id-4335079.html
输入./configure --help
能够查看Nginx全部支持配置的内置模块的配置信息。其中:
--with
开启。--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
(完)