Nginx核心知识100讲 第三部分课件
Nginx核心知识100讲 第三部分课件
/
main
http {
upstream { … }
split_clients {…}
map {…}
geo {…}
server {
if () {…}
location {
limit_except {…}
}
location {
location {
}
}
}
server {
}
}
Context
location / {
}
HTTP
listen unix:/var/run/nginx.sock;
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000 bind;
listen [::]:8000 ipv6only=on;
listen [::1];
SYN
SYN+ACK CPU
worker
ACK epoll_wait
accept
ngx_http_init_connection
connection_pool_size: epoll_ctl
512
client_header_timeout: 60s
DATA
ACK
epoll_wait ngx_http_wait_request_handler read
client_header_buffer_size:1k
HTTP
URI
header
header
request_pool_size:4k
large_client_header_buffers:4 8k
header
large_client_header_buffers:4 8k
client_header_timeout: 60s
11 http
URI
Syntax: client_header_buffer_size size;
url /admin/website/article/35/change/uploads/party/5.jpg
url /static/uploads/party/5.jpg
url
/^\/admin\/website\/article\/(\d+)\/change\/uploads\/(\w+)\/(\w+)\.(png|jpg|gif|jpeg|b
mp)$/
rewrite^/admin/website/solution/(\d+)/change/uploads/(.*)\.(png|jpg|gif|jpeg|bmp)$
/static/uploads/$2/$3.$4 last;
server_name
0
Syntax server_name_in_redirect on | off;
Default server_name_in_redirect off;
Context http, server, location
3 2
server_name *.taohui.tech
1 *
server_name www.taohui.tech ~^www\d+\.taohui\.tech$;
server_name
0 (4 )
server { server {
server_name ~^(www\.)?(.+)$; server_name ~^(www\.)?(?<domain>.+)$;
location / { root /sites/$2; } location / { root /sites/$domain; }
} }
21 5
01 * 0
05 02 * 0
0 5 34
04 03
default server
• 1
• listen default
HTTP 11
POST_READ realip
SERVER_REWRITE rewrite
FIND_CONFIG
REWRITE rewrite
POST_REWRITE
POST_ACCESS
PRECONTENT try_files
log log
IP
03 HTTP X-Real-IP IP
CDN
X-Forwarded-For 115.204.33.1 IP 1.1.1.1
04 X-Real-IP 115.204.33.1
IP 2.2.2.2
X-Forwarded-For 115.204.33.1 1.1.1.1
X-Real-IP 115.204.33.1
Nginx 115.204.33.1
remote_addr 2.2.2.2
IP
binary_remote_addr remote_addr
IP limit_conn
realip
Nginx
• --with-http_realip_module
• set_real_ip_from
• real_ip_header
• real_ip_recursive
• realip_remote_addr
• realip_remote_port
realip
log log
Rewrite return
• Nginx
• HTTP 1.1
• 303
• 307
• 308
return
server {
server_name return.taohui.tech;
listen 8080;
server location
return
root html/;
error_page 404 /403.html;
return 403; return error_page
location /{
return 404 "find nothing!";
}
}
rewrite return error_page
location /second {
rewrite /second(.*) /third$1 break;
return 200 'second!'; . / 3
}
2 2
location /third { 3
return 200 'third!'; 3
}
rewrite
location /redirect1 {
rewrite /redirect1(.*) $1 permanent;
}
/redirect1/index.html
location /redirect2 {
rewrite /redirect2(.*) $1 redirect;
} 2 3 4
location /redirect3 {
rewrite /redirect3(.*) http://rewrite.taohui.tech$1;
}
location /redirect4 {
rewrite /redirect4(.*) http://rewrite.taohui.tech$1 permanent;
}
rewrite error
02 = !=
03
• ~ !~
• ~* !~*
04 -f !-f
05 -d !-d
06 -e !-e
07 -x !-x
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
if if ($request_method = POST) {
return 405;
}
if ($slow) {
limit_rate 10k;
}
if ($invalid_referer) {
return 403;
}
11 char *ngx_module_names[] = {
……
"ngx_http_static_module",
realip postread "ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_random_index_module",
"ngx_http_mirror_module",
rewrite find_config rewrite "ngx_http_try_files_module",
"ngx_http_auth_request_module",
"ngx_http_auth_basic_module",
limit_req limit_conn preaccess "ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_realip_module",
access auth_basic auth_request access "ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_concat_module",
try_files mirrors precontent ……
}
log log
location
•
• =
/ • ^~
• merge_slashes on
• ~
location
• ~*
• @
location ~ /Test1/$ {
return 200 'first regular expressions match!';
}
location ~* /Test1/(\w+)$ {
return 200 'longest regular expressions match!';
}
location ^~ /Test1/ {
/Test1 return 200 'stop regular expressions match!';
}
/Test1/
location /Test1/Test2 {
return 200 'longest prefix string match!';
/Test1/Test2 }
location = /Test1 {
return 200 'exact match!';
}
location
location
= ^~
= location ^~ location
location
nginx.conf location
location
11 char *ngx_module_names[] = {
……
"ngx_http_static_module",
realip postread "ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_random_index_module",
"ngx_http_mirror_module",
rewrite find_config rewrite "ngx_http_try_files_module",
"ngx_http_auth_request_module",
"ngx_http_auth_basic_module",
limit_req limit_conn preaccess "ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_realip_module",
access auth_basic auth_request access "ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_concat_module",
try_files mirrors precontent ……
}
log log
ngx_http_limit_conn_module
Ø NGX_HTTP_PREACCESS_PHASE
Ø http_limit_conn_module
Ø nginx --without-http_limit_conn_module
• worker
• preaccess
2 0 key
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
1
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
limit_conn
Ø NGX_HTTP_PREACCESS_PHASE
Ø http_limit_req_module
Ø nginx --without-http_limit_req_module
Ø leaky bucket
• worker
• preaccess
nodelay
11 char *ngx_module_names[] = {
……
"ngx_http_static_module",
realip postread "ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_random_index_module",
"ngx_http_mirror_module",
rewrite find_config rewrite "ngx_http_try_files_module",
"ngx_http_auth_request_module",
"ngx_http_auth_basic_module",
limit_req limit_conn preaccess "ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_realip_module",
access auth_basic auth_request access "ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_concat_module",
try_files mirrors precontent ……
}
log log
IP
Nginx
GET / HTTP/1.1
auth_basic
htpasswd
# comment
name1:password1
name2:password2:comment httpd-tools
name3:password3 htpasswd –c file –b user pass
auth_request
2xx
401 403 Syntax: auth_request uri | off;
Default: auth_request off;
Context: http, server, location
satisfy satisfy
access
• access
all any
• auth_basic
any all
• auth_request
• access access
2 return access
1 access 5 43
ngx_modules.c location /{
satisfy any;
• &ngx_http_auth_request_module, auth_basic "test auth_basic";
• &ngx_http_auth_basic_module, auth_basic_user_file examples/auth.pass;
deny all;
• &ngx_http_access_module,
}
log log
precontent try_files
URL code
precontent mirror
ngx_http_mirror_module
Nginx
--without-http_mirror_module
alias location
URL
location /root {
root html;
}
html/first/
└── 1.txt location /alias {
alias html;
}
URL
location ~ /root/(\w+\.txt) {
/root root html/first/$1;
}
/alias
location ~ /alias/(\w+\.txt) {
/root/1.txt
alias html/first/$1;
/alias/1.txt }
/RealPath/1.txt
[error] 10156#0: *10723 open() "/html/first/2.txt/root/2.txt" failed (2: No such file or directory)
URL /
static root/alias
URL / 301
Syntax: server_name_in_redirect on | off;
Default: server_name_in_redirect off;
Context: http, server, location
/ index
ngx_http_index_module
index index
/
URL /
ngx_http_index_module
html/xml/json/jsonp
Nginx
root/alias
--without-http_autoindex_module
autoindex
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
Syntax: autoindex_format html | xml | json | jsonp;
Default: autoindex_format html;
Context: http, server, location
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
content concat
ngx_http_concat_module
Tengine(https://github.com/alibaba/nginx-http-concat)
--add-module=../nginx-http-concat/
uri ?? , ?
https://g.alicdn.com/??kissy/k/6.2.4/seed-min.js,kg/global-util/1.0.7/index-
min.js,tb/tracker/4.3.5/index.js,kg/tb-nav/2.5.3/index-min.js,secdev/sufei_data/3.3.5/index.js
concat
concat on | off concat_delimiter string
default concat off Default NONE
Context http, server, location Context http, server, locatione
concat_max_files numberp
Default concat_max_files 10
Context http, server, location
11 char *ngx_module_names[] = {
……
"ngx_http_static_module",
realip postread "ngx_http_autoindex_module",
"ngx_http_index_module",
"ngx_http_random_index_module",
"ngx_http_mirror_module",
rewrite find_config rewrite "ngx_http_try_files_module",
"ngx_http_auth_request_module",
"ngx_http_auth_basic_module",
limit_req limit_conn preaccess "ngx_http_access_module",
"ngx_http_limit_conn_module",
"ngx_http_limit_req_module",
"ngx_http_realip_module",
access auth_basic auth_request access "ngx_http_referer_module",
"ngx_http_rewrite_module",
"ngx_http_concat_module",
try_files mirrors precontent ……
}
log log
log log
HTTP ngx_http_log_module
access
combined
Ø path cache
Ø if
Ø
•
•
•
• flush
• worker reopen
Ø
•
• buffer 64KB
• 1 1 9
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
Syntax:
open_log_file_cache off;
Default: open_log_file_cache off;
Context: http, server, location
max LRU
inactive 10
valid valid 60
off
limit_req zone=req_one
HTTP
burst=120;
preaccess limit_req limit_conn limit_conn c_zone 1;
HTTP
copy_filter:
HTTP
postpone_filter:
HTTP
header_filter:
write_filter:
sub
ngx_http_sub_filter_module
Nginx
--with-http_sub_module
sub
Syntax: sub_filter string replacement;
Default: —
Context: http, server, location
ngx_http_addition_filter_module
Nginx
--with-http_addition_module
url
addition
Preconfiguration
http access
HTTP nginx.conf
HTTP 2
HTTP 3 2
TCP 3 2
Nginx 0 4 12
HTTP 3 2
Nginx 5
HTTP
arg_ URL
query_string args
args URL
is_args URL ?
content_type Content-Type
HTTP
document_uri uri
request_length
request_body_file
Ø
•
• client_body_in_file_only
request_body
request
url GET /?a=1&b=22 HTTP/1.1
HTTP
Host
host
server_name
HTTP
• http_host
• http_user_agent
http_ • http_referer
• http_via
• http_x_forwarded_for
• http_cookie
TCP
connection
connection_requests keepalive
remote_addr
remote_port
proxy_protocol_addr proxy_protocol
TCP
proxy_protocol_port proxy_protocol
server_addr
server_port
server_protocol HTTP/1.1
Nginx
request_time
server_name
server_name
https
TLS/SSL on
request_completion
OK
request_id
16 id id 16
Nginx
request_filename
document_root
URI root/alias
realpath_root
document_root
limit_rate
set
HTTP
body_bytes_sent sent_http_
• body Ø
bytes_sent • sent_http_content_type
• sent_http_content_length
• http • sent_http_location
status • sent_http_last_modified
• sent_http_connection
• http • sent_http_keep_alive
sent_trailer_ • sent_http_transfer_encoding
• sent_http_cache_control
• • sent_http_link
Ø
Nginx
nginx_version Nginx
pid worker id
pipe p .
hostname hostname
hostname 1970 1 1
referer
Ø daro _ x Ø w _dahv a _
f l _ m
ro da
x_ i
l p x n_
Ø ro - Ø g ro
u te N
referer
none • 1
• referer
block • 1
• referer
server_names
• referer server_name
URL
*
• referer
• referer
server_name referer.taohui.tech;
Ø nginx
url
• secure_link
Ø url nginx nginx
• secure_link_expires secure_link
Ø
Ø URL
Ø URL URL nginx
ngx_http_secure_link_module
Ø
nginx • HTTP URI
URL
--with-http_secure_link_module • IP URL
• URL
•
secure_link
secure_link_expires
Ø
• /test1.txt?md5=md5 &expires= 2147483647
Ø md5
• echo -n ‘ URL IP ' | openssl md5 -binary | openssl base64 | tr +/ - | tr -d =
Nginx
Ø secure_link $arg_md5,$arg_expires;
Ø secure_link_md5 "$secure_link_expires$uri$remote_addr secret";
URI 1
0 URL
/prefix/hash/link
2 Hash 3
• link md5
secure_link_secret secret;
URI
Ø
• link
Ø
• /prefix/md5/link
Ø md5
• echo -n 'linksecret' | openssl md5 –hex
Nginx
Ø secure_link_secret secret;
map
switch
ngx_http_map_module
{case: … default: …}
Nginx
--without-http_map_module
map
• • m
• o f n •
• n a c m
•
c m
• m f
• m h
• i
• e l • * n d
n
map $http_host $name {
hostnames;
default 0;
map
name ~map\.tao\w+\.org.cn 1;
*.taohui.org.cn 2;
• 'Host: map.taohui.org.cn’
map.taohui.tech 3;
• 'Host: map.tao123.org.cn’ map.taohui.* 4;
}
• 'Host: map.taohui.pub’
map $http_user_agent $mobile {
• 'Host: map.taohui.tech’
default 0;
"~Opera Mini" 1;
}
AB split_clients
ngx_http_split_clients_ Ø MurmurHash2
32 hash
module Ø 32 2^32-1
Nginx —without- max
Ø hash/max
http_split_clients_module percent
Ø
0-1%,1%-5%
Ø percent
case
• • xx.xx% 2 100%
•
• * 100%
•
split_clients
Syntax: split_clients string $variable { ... }
Default: —
Context: http
-
-
geo
• default
• proxy realip remote_addr X-
Forwarded-For IP
• proxy_recursive
• include
• delete
geo
geo $country {
default ZZ; country
proxy
#include conf/geo.conf;
Ø curl -H 'X-Forwarded-For: 10.1.0.0,127.0.0.2'
proxy 116.62.160.193;
geo.taohui.tech
Ø curl -H 'X-Forwarded-For: 10.1.0.0,127.0.0.1'
127.0.0.0/24 US;
geo.taohui.tech
127.0.0.1/32 RU; Ø curl -H 'X-Forwarded-For:
10.1.0.0/16 RU; 10.1.0.0,127.0.0.1,1.2.3.4' geo.taohui.tech
192.168.1.0/24 UK;
}
MaxMind geoip
Ø MaxMind geoip C
IP
https://dev.maxmind.com/geoip/
legacy/downloadable/
Ø nginx --with-
http_geoip_module
Ø MaxMind
Ø geoip_country geoip_city
ngx_http_geoip_module
nginx.conf
nginx
Ø nginx
--with-http_geoip_module
geoip_country
Ø Connection close
HTTP TCP
keepalive
•
•
Ø Keep-Alive timeout=n
n
• TCP
n
keepalive