2-1
中間件是什麼?
NGINX是一個開源且高性能,可靠的HTTP中間件,代理服務。
2-2
常見的HTTP服務:HTTPD,IIS,GWS
2-3/4/5/6
爲什麼選擇nginx?
- IO多路複用epoll:(多個描述符的I/O一個線程裏併發交替的完成),實現併發和吞吐
- 輕量級:功能模塊少(出於性能考慮,源代碼只保留核心代碼),代碼模塊化(易讀,可以二次改進,比如阿里的tenginx)
- CPU親和:一個worker進程在一個CPU上進行
- sendfile:靜態文件不需要過多的用戶空間邏輯處理,可以直接內核空間進行傳輸。
2-7
http://nginx.org下載安裝包
2-8
安裝目錄的講解
|-- client_body_temp
|-- conf #這是Nginx所有配置文件的目錄,極其重要
| |-- fastcgi.conf #fastcgi相關參數的配置文件
| |-- fastcgi.conf.default #fastcgi.conf的原始備份
| |-- fastcgi_params #fastcgi的參數文件
| |-- fastcgi_params.default
| |-- koi-utf # 編碼轉換映射轉化文件(很少用到)
| |-- koi-win #同上
| |-- mime.types #媒體類型,
| |-- mime.types.default
| |-- nginx.conf #這是Nginx默認的主配置文件
| |-- nginx.conf.default
| |-- scgi_params #scgi相關參數文件,一般用不到
| |-- scgi_params.default
| |-- uwsgi_params #uwsgi相關參數文件,一般用不到
| |-- uwsgi_params.default
| |-- win-utf # 編碼轉換映射轉化文件(很少用到)
|-- fastcgi_temp #fastcgi臨時數據目錄
|-- html #這是編譯安裝時Nginx的默認站點目錄,類似
Apache的默認站點htdocs目錄
| |--50x.html # 錯誤頁面優雅替代顯示文件,例如:出現502錯誤時會調用此頁面
# error_page 500502503504 /50x.html;
| |-- index.html # 默認的首頁文件,首頁文件名字是在nginx.conf中事先定義好的。
|-- logs #這是Nginx默認的日誌路徑,包括錯誤日誌及訪問日誌
| |-- access.log # 這是Nginx的默認訪問日誌文件,使用tail -f access.log,可以實時觀看網站用戶訪問情況信息
| |-- error.log # 這是Nginx的錯誤日誌文件,如果Nginx出現啓動故障等問題,一定要看看這個錯誤日誌
| |-- nginx.pid # Nginx的pid文件,Nginx進程啓動後,會把所有進程的ID號寫到此文件
|-- proxy_temp #臨時目錄
|-- sbin #這是Nginx命令的目錄,如Nginx的啓動命令nginx
| |-- nginx #Nginx的啓動命令nginx
|-- scgi_temp #臨時目錄
|-- uwsgi_temp #臨時目錄
2-9
編譯配置參數的講解
nginx -V :會出現如下信息
-prefix=/etc/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log 安裝目錄或者路徑
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
------------------------------------------------------------------------------------------------------------------------------
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 執行對應模塊時,Nginx所保留的臨時性文件
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
------------------------------------------------------------------------------------------------------------------------------
--user=nginx
--group=nginx 設定Nginx的進程啓動的用戶和組用戶
------------------------------------------------------------------------------------------------------------------------------
--with-cc-opt C語言編譯
------------------------------------------------------------------------------------------------------------------------------
--with-ld-opt= 設置附加的參數,鏈接系統庫
2-10
默認配置基本語法
查看CPU核數:cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
子配置文件:
從上到下依次:
列 作用
listen 80; 監聽的端口
server_name localhost; 用域名方式訪問的地址
------------------------------------------------------------------------------------------------------------------------------
location / { 一個server裏可以有多個location,當這是/的時候,
root /usr/share/nginx/html; root是存放首頁的路徑
index index.html index.htm; 訪問的頁面,訪問index.html
}
------------------------------------------------------------------------------------------------------------------------------
error_page 500 502 503 504 404 /50x.html; 當訪問錯誤的時候,會顯示的頁面
location = /50x.html {
root /usr/share/nginx/html; root是存放頁面的路徑
}
2-11
2-12
HTTP 請求
安裝curl:
yum install curl
curl -v www.baidu.com
2-13
日誌:
access log 記錄用戶,頁面以及用戶瀏覽器,ip和其他的訪問信息
85.14.245.124 - - [15/May/2019:12:14:02 +0000] "\x03\x00\x00/*\xE0\x00\x00\x00\x00\x0
0Cookie: mstshash=Administr" 400 157 "-" "-"
104.152.52.68 - - [15/May/2019:12:28:28 +0000] "GET / HTTP/1.0" 200 612 "-" "-"
187.11.247.100 - - [15/May/2019:15:19:10 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla
/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Vers
ion/9.1.2 Safari/601.7.7"
193.188.22.116 - - [15/May/2019:15:29:09 +0000] "\x03\x00\x00/*\xE0\x00\x00\x00\x00\x
00Cookie: mstshash=Administr" 400 157 "-" "-"
error log 記錄服務器錯誤日誌
2019/05/15 20:26:45 [error] 102452#0: *22 open() "/usr/local/nginx/html/cache/global/
img/gs.gif" failed (2: No such file or directory), client: 80.82.70.187, server: loca
lhost, request: "GET http://www.baidu.com/cache/global/img/gs.gif HTTP/1.1", host: "w
ww.baidu.com"
2019/05/15 21:29:41 [error] 102452#0: *24 open() "/usr/local/nginx/html/robots.txt" f
ailed (2: No such file or directory), client: 124.43.17.15, server: localhost, reques
t: "GET /robots.txt HTTP/1.1", host: "42.159.195.188"
2019/05/15 21:29:41 [error] 102452#0: *24 open() "/usr/local/nginx/html/Appbcc39f2a.p
hp" failed (2: No such file or directory), client: 124.43.17.15, server: localhost, r
equest: "POST /Appbcc39f2a.php HTTP/1.1", host: "42.159.195.188"
2-14
vim nginx.conf
access日誌格式配置:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$upstream_addr $upstream_response_time $request_time ';
access_log logs/access.log main;
#配置access log日誌的存儲位置及文件,注意:access.log文件是可以按日期進行分割的,方便查看及處理
access_log /usr/local/nginx/log/access.log main;
相關說明解釋
1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
2.$remote_user :用來記錄客戶端用戶名稱;
3.$time_local : 用來記錄訪問時間與時區;
4.$request : 用來記錄請求的url與http協議;
5.$status : 用來記錄請求狀態;成功是200,
6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
8.$http_user_agent :記錄客戶端瀏覽器的相關信息;
2-15
模塊講解:
官方模塊
第三方模塊
2-16
sub_status:NGINX的客戶端狀態