經常編譯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_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;
}
參考:
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
(完)