一勋拟、nginx特性
1、IO
2妈候、輕量級
3敢靡、CPU親和(affinity)
cpu親和:是一種把CPU核心和nginx工作進程綁定方式,把每個worker進程固定在一個cpu上執(zhí)行苦银,減少切換的cachemiss,獲得良好的性能啸胧。
4、sendfile
處理靜態(tài)文件效率很高幔虏,因為他的傳輸機制是:sendfile纺念。
對比之前httpserver的傳輸文件。httpserver傳輸機制
文件--》操作系統(tǒng)內(nèi)核空間-->用戶空間--》socket;響應給用戶想括。 發(fā)生多次切換
文件 其實不需要經(jīng)過用戶空間(處理邏輯)陷谱,直接就可以通過內(nèi)核空間傳輸。sendfile就是只通過內(nèi)核空間 進行傳輸瑟蜈。
二烟逊、nginx快速搭建
參考官方文檔介紹
三、nginx的目錄和配置語法
基礎(chǔ)參數(shù)使用
省略
四铺根、Http請求
請求報文:
request- 包括請求行宪躯、請求頭部、請求數(shù)據(jù)
response-包括狀態(tài)行位迂、消息報頭访雪、響應正文
下面來查詢一下一次請求的內(nèi)容信息。curl -v http://www.imooc.com
[root@localhost conf]# curl -v http://www.imooc.com
* About to connect() to www.imooc.com port 80 (#0)
* Trying 117.121.101.40...
* Connected to www.imooc.com (117.121.101.40) port 80 (#0)
> GET / HTTP/1.1 //... >發(fā)起request請求 請求方式掂林,請求協(xié)議
> User-Agent: curl/7.29.0 //... request請求的head內(nèi)容
> Host: www.imooc.com
> Accept: */*
> //... < response 服務端響應的內(nèi)容
< HTTP/1.1 301 Moved Permanently // 301 響應的狀態(tài)
< Server: nginx
< Date: Mon, 14 May 2018 08:38:35 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: https://www.imooc.com
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.imooc.com left intact
五臣缀、nginx日志類型
暫且省略,參考官網(wǎng)
六党饮、nginx模塊講解
1. nginx官方模塊
http_stub_status_module
random_index_module
Syntax:random_index on | off;
Default:random_index off;
Context:location
http_sub_module :http內(nèi)容替換 (有多個語法)
Syntax:sub_filter string replacement;
Default:-
Context:http,server,location
2.第三方模塊
七肝陪、nginx的請求限制
連接頻率限制 : limit_conn_module
請求頻率限制 : limit_req_module
1.1連接與請求的原理
建立在TCP協(xié)議之上。 TCP三次請求之后刑顺,才能進行http之間連接請求氯窍。最后在通過客戶端與服務端之間第三方包來回傳輸饲常。
HTTP請求建立在一次TCP連接基礎(chǔ)上
一次TCP請求至少產(chǎn)生一次HTTP請求,當然可以產(chǎn)生多次HTTP請求
1.2 nginx對http請求連接的限制語法
Syntax:limit_conn_zone key zone=name:size;
Default:-
Context:http
Syntax:limit_conn_zone number;
Default:-
Context:http,server,location
1.3請求限制
Syntax:limit_req_zone key zone=name:size rate=rate;
Default:-
Context:http
Syntax:limit_req zone=name [burst=number][nodelay];
Default:-
Context:http,server,location
八狼讨、nginx的訪問控制
1贝淤、實現(xiàn)訪問控制的基本方式
? 基于IP的訪問控制 http_access_module
? 基于用戶的信任登錄 http_auth_basic_module
2、http_access_module
Syntax:allow address|CIDR|unix:|all;
Default:-
Context:http,server,location,limit_except
Syntax:deny address|CIDR|unix:|all;
Default:-
Context:http,server,location,limit_except
http_access_module 配置
http_access_module 局限性
? 只能通過remote_addr來信任政供。
優(yōu)化
3播聪、http_x_forwarded_for
記錄過程和ip地址
http_x_forwarded_for=ClientIP,Proxy(1)IP,Proxy(2)IP... ...
4、http_access_module局限性
方法一布隔、采用別的HTTP信息控制訪問离陶,如:HTTP_X_FORWARD_FOR
方法二、結(jié)合geo模塊作(后面詳細記錄)
方法三衅檀、通過HTTP自定義變量傳遞