nginx other modules

最後更新: 2019-05-09

 

介紹

所有 nginx 的 3 party module list

Link: https://www.nginx.com/resources/wiki/modules/

目錄

常用的 module

3rdPartyModules

 


Check 本地的 nginx 支援什麼 Modules

 

nginx -V

nginx version: nginx/1.14.2
configure arguments: ... 
--modules-path=/usr/lib/nginx/modules
...
--with-http_ssl_module
...
--add-dynamic-module=/build/nginx-tBUzFN/nginx-1.14.2/debian/modules/http-auth-pam
...

 


ngx_http_stub_status_module

 

Setting

location /status {
  stub_status     on;
  allow           x.x.x.x;   # x.x.x.x = monitoring system
  deny            all;
}

Output:

Active connections: 4 
server accepts handled requests
 41 41 93 
Reading: 0 Writing: 2 Waiting: 2 

 

  • Active connections - number of active client connections(including Waiting connections)

 

  • accepts(Total)
  • handled(Total) - value is the same as accepts unless some resource limits have been reached
  • requests(Total)

 

  • reading - nginx reads request header
  • writing - nginx is writing the response back to the client
  • waiting - "active - (reading + writing)"

 


ngx_http_log_module

 

Setting

  • access_log
  • log_format

 * error_log 不是由 ngx_http_log_module 提供

Syntax:

access_log

    access_log path [ format [ buffer = size [ flush = time ]]]
    access_log path format gzip[= [ buffer = size ] [ flush = time ]
    access_log off

    Default:     logs/access.log combined

log_format

usage: log_format name string

e.g. log_format

log_format apache
    '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$http_cookie"';
    
log_format full
    '$remote_addr $remote_user [$time_local] '
    '"$host"->$proxy_host->$upstream_addr '
    '"$request" $status($upstream_status) '
    '$bytes_sent/$gzip_ratio($sent_http_content_type) '
    '$request_time($upstream_response_time)';
    
log_format perf
    '$request_time($upstream_response_time) '
    '$bytes_sent/$gzip_ratio($sent_http_content_type) '
    '$status "$upstream_addr$uri"';
    
log_format gzip
    '$bytes_sent/$gzip_ratio($sent_http_content_type) '
    '[$http_accept_encoding]"$http_user_agent"';

log_format redirect
    '$time_local $redir_match $redir_action $redir_url';

log_format cache_status '[$time_local] "$request"  $upstream_cache_status';

e.g. access_log

#access_log off;
access_log            /var/log/nginx/access.log       apache;
#access_log           /var/log/nginx/access-full.log  full;
access_log            /var/log/nginx/access-perf.log  perf;
#access_log           /var/log/nginx/access-gzip.log  gzip;
access_log            /var/log/nginx/cache.log        cache_status;

cache_status output:

[25/Jan/2014:16:09:12 +0000] "GET /node/103 HTTP/1.0"  MISS
[25/Jan/2014:16:09:13 +0000] "GET /guestbook HTTP/1.0"  MISS
[25/Jan/2014:16:09:14 +0000] "GET /comment/reply/103 HTTP/1.0"  MISS

buffer & flush 設定

# 儲夠 64k 或 每 5 秒寫 1 次 log

access_log  /var/log/tengine/myserver/cache.log cache_status buffer=64k flush=5;

if 的設定

map $status $loggable {
    ~^[23]  0;
    default 1;
}

access_log /path/to/access.log combined if=$loggable;

我的 Proxy log

log_format cache_status '[$time_local] "$remote_addr" '
                        '"$request" $status $upstream_cache_status '
                        '* $request_time $bytes_sent "$gzip_ratio"';

Variables

$request_time

request processing time in seconds with a milliseconds resolution;

time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client  

$bytes_sent

the number of bytes sent to a client

$status

Status code. i.e. 200

header to client

Header lines sent to a client have the prefix "sent_http_",

for example, $sent_http_connection

error_log

# stop error log ("error_log off;" 這是唔 work 的, 沒有 off 這設定)

error_log   /dev/null   crit;

 


ngx_http_degradation_module

 

Compile

--with-http_degradation_module

# Allows returning an error when a memory size exceeds the defined value.

# to be inserted at the http block level
degradation sbrk=500m;
degrade 204;

 


HttpHeadersModule

 

# Default: off, prevents changes to the Expires and Cache-Control headers.

# max: Expires header to 31 December 2037 23:59:59 GMT, Cache-Control max-age to 10 years.

expires       24h;

# Cache-Control: no-cache

expires       -1;

add_header    Cache-Control  private;

 


ngx_http_charset_module

 

# “Content-Type” response header field.
charset UTF-8;

 


ngx_http_map_module

 

Creates a new variable whose value depends on values of one or more of the source variables (first parameter)

Usage:

map string $variable { ... }

P.S.

Context: http

regular expression:

"~"           case-sensitive matching,
"~*"         case-insensitive matching.

e.g.

當 $request_method 是 PURGE 時會把 $purge_method 設定成 $purge_allowed

map $request_method $purge_method {
    PURGE   $purge_allowed;
    default 0;
}