ngx_http_access_module
基于ip的访问控制
ngx_http_auth_basic_module
基于用户的访问控制
ngx_http_stub_status_module
输出nginx的基本信息
ngx_http_log_module
指定日志格式记录请求
ngx_http_gzip_module
用gzip方法压缩响应数据
ngx_http_ssl_module
是否使用加密传输
ngx_http_rewrite_module
对用户请求完成重定向替换
ngx_http_proxy_module
代理模块
ngx_http_headers_module
响应报文头部
ngx_http_fastcgi_module
fastcgi 模块
Syntax: allow address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except
location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; }
ocation / { auth_basic "closed site"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; }
Syntax: auth_basic string | off; Default: auth_basic off; Context: http, server, location, limit_except
Syntax: auth_basic_user_file file; Default: — Context: http, server, location, limit_except
name1:password1 name2:password2:comment name3:password3
Syntax: stub_status; Default: — Context: server, location
location /basic_status { stub_status; }
Active connections: 2 server accepts handled requests 19 19 14 Reading: 0 Writing: 1 Waiting: 1
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数
log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /var/log/nginx/nginx-access.log compression buffer=32k;
Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http
$bytes_sent
: 返回给客户端的字节数
$connection
: 链接请求的序号
$request_length
: 请求的长度,包含(请求线,请求头和请求体)
$request_time
: 整个请求所经历的时间。
$status
: 响应的状态
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except
Syntax: gzip on | off; Default: gzip off; Context: http, server, location, if in location Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location
http { ... server { listen 443 ssl; keepalive_timeout 70; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5; ssl_certificate /usr/local/nginx/conf/cert.pem; ssl_certificate_key /usr/local/nginx/conf/cert.key; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ... } ... }
1、rewrite regex replacement [flag]
Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if
2、return
Syntax: return code [text]; return code URL; return URL; Default: — Context: server, location, if
3、rewrite_log on | off;
Syntax: rewrite_log on | off; Default: rewrite_log off; Context: http, server, location, if
4、if (condition) { … }
Syntax: if (condition) { ... } Default: — Context: server, location
==相同
!= 不同
~:模式匹配,区分字符大小写
~*:模式匹配,不区分字符大小写
!~:模式不匹配,区分字符大小写
!~*:模式不匹配,不区分字符大小写
-e, !-e 存在(包括文件,目录,软链接)
-f, !-f 文件
-d, !-d 目录
-x, !-x 执行
Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except
server { ... server_name HOSTNAME; location /uri/ { proxy_pass http://host[:port]; # 最后没有/ } ... }
server { ... server_name HOSTNAME; location /uri/ { proxy_pass http://host/new_uri/; } ... }
server { ... server_name HOSTNAME; location ~|~* /uri/ { proxy_pass http://host; # 不能加/ } ... }
Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close; Context: http, server, location
Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http
Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location
Syntax: proxy_cache_key string; Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location
Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
proxy_cache proxycache; # 这个proxycache 就是在前面key_zone 中定义的名字 proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; # 每种响应码的缓存时间 proxy_cache_valid any 1m;
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; Default: proxy_cache_use_stale off; Context: http, server, location
Syntax: proxy_cache_methods GET | HEAD | POST ...; Default: proxy_cache_methods GET HEAD; Context: http, server, location
Syntax: proxy_hide_header field; Default: — Context: http, server, location
Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location
Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location
Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location
Syntax: add_header name value [always]; Default: — Context: http, server, location, if in location
add_header X-Via $server_addr; add_header X-Cache $upstream_cache_status; add_header X-Accel $server_nam
Syntax: add_trailer name value [always]; Default: — Context: http, server, location, if in location
Syntax: fastcgi_pass address; Default: — Context: location, if in location
Syntax: fastcgi_index name; Default: — Context: http, server, location
Syntax: fastcgi_param parameter value [if_not_empty]; Default: — Context: http, server, location
location ~* \.php$ { fastcgi_pass 后端fpm服务器IP:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /app/php$fastcgi_script_name; include fastcgi_params; … }
location ~* ^/(pm_status|ping)$ { include fastcgi_params; fastcgi_pass 后端fpm服务器IP:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; }
Syntax: fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http
Syntax: fastcgi_cache zone | off; Default: fastcgi_cache off; Context: http, server, location
Syntax: fastcgi_cache_key string; Default: — Context: http, server, location
Syntax: fastcgi_cache_methods GET | HEAD | POST ...; Default: fastcgi_cache_methods GET HEAD; Context: http, server, location
Syntax: fastcgi_cache_min_uses number; Default: fastcgi_cache_min_uses 1; Context: http, server, location
Syntax: fastcgi_keep_conn on | off; Default: fastcgi_keep_conn off; Context: http, server, location
Syntax: fastcgi_cache_valid [code ...] time; Default: — Context: http, server, location