在nginx以前
同步異步阻塞非阻塞、併發並行
1)同步與異步html
- 同步:當一個同步調用發出後,調用者要一直等待調用的結果返回,才能進行後續的執行
- 異步:當一個異步調用發出後,調用者沒必要一直等待調用結果的返回;異步調用要獲取結果,有兩種方式:
- 主動輪詢異步調用的結果
- 被調用方經過callback(回調通知)來通知調用方調用結果
2)阻塞與非阻塞linux
阻塞與非阻塞的重點在於進/線程等待消息時的行爲:在等待消息的時候,當前線程是掛起狀態,仍是非掛起狀態nginx
- 阻塞:調用發出後,在消息返回前,當前線程被掛起,直到有消息返回,當前進/線程纔會被激活
- 非阻塞:調用發出後,不會阻塞當前進/線程,而會當即返回
總結:同步與異步,重點在於消息通知的方式;阻塞與非阻塞,重點在等待消息時的行爲。c++
Nginx採用異步非阻塞的方式工做。跟Linux中的epoll模型有關。web
3)併發與並行正則表達式
併發(concurrency)和並行(parallellism)是:docker
- 解釋一:並行是指兩個或者多個事件在同一時刻發生;而併發是指兩個或多個事件在同一時間間隔發生。
- 解釋二:並行是在不一樣實體上的多個事件,併發是在同一實體上的多個事件。
- 解釋三:在一臺處理器上「同時」處理多個任務,在多臺處理器上同時處理多個任務。
如hadoop分佈式集羣 因此併發編程的目標是充分的利用處理器的每個核,以達到最高的處理性能shell
epoll
I/O多路複用的epoll模型apache
當有I/O事件產生時,epoll就會去告訴進程哪一個連接有I/O事件,而後進程就去處理這個事件編程
nginx原理
每進來一個request,會有一個worker進程去處理。但不是全程處理,處理到可能發生阻塞的地方。好比向後端服務器轉發request,並等待結果返回。那麼,這個worker不會這麼傻等着,他會在發完請求後,註冊一個事件:「若是後端服務返回了,告訴我一聲,我接着幹」。因而他休息去了。此時,若是再有新的request進來,他就能夠很快再以這種方式處理。而一旦後端服務返回了,就會觸發這個事件,worker纔會來接手,這個request都會接着往下走。經過這種快速處理,快速釋放請求的方式,達到一樣的配置能夠處理更大併發量的目的。
概述
nginx是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。
nginx支持fastCGI(默認支持)
nginx有兩種工做模式
- master-worker模式
- 單進程模式
master-worker模式:有一個master進程和至少一個worker進程。master進程負責處理系統信號、加載配置、管理worker進程(啓動、殺死、監控、發送消息/信號等)。worker進程負責處理具體業務邏輯,也就是說,真正提供服務的是worker進程。生產環境下通常使用這種模式。
單進程模式:todo...
模塊分類
- 核心模塊:HTTP模塊、EVENT模塊和MAIL模塊
- 基礎模塊:HTTP Access、HTTP FastCGI、HTTP Proxy、HTTP Rewrite
- 第三方模塊:HTTP Upstream Request Hash、Notive、HTTP Access Key
nginx的高併發利益於採用epoll模型,epoll模型是linux2.6之後纔出現,apache採用select模型。
應用場景
三大應用場景
- 靜態資源服務
- 反向代理服務
- API服務
安裝(linux)
安裝環境:CentOS 7
方式一:make
-
安裝nginx依賴的其它包
- pcre-devel,nginx的配置文件中使用了正則表達式,所以要安裝來支持。
- 還有其它的依賴,如gcc、gcc-c++、zlib-devel、openssl-devel等,根據須要來安裝(通常都建議安裝上)。
-
安裝 nginx
$ wget http://nginx.org/download/nginx-1.12.1.tar.gz $ tar -zxvf nginx-1.12.1.tar.gz $ cd nginx-1.12.1/ $ ./configure $ make $ sudo make install
默認安裝在/usr/local/nginx
,也能夠在./configure
時指定安裝的路徑./configure --prefix=/home/geek/nginx
默認安裝的log
和conf
都在/usr/local/nginx
下面
/usr/local/nginx
目錄以下:
├── conf │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── html │ ├── 50x.html │ └── index.html ├── logs │ ├── access.log │ ├── error.log │ └── nginx.pid ├── sbin │ └── nginx
源碼安裝官方文檔:http://nginx.org/en/docs/configure.html
- 啓動 & 關閉
$ sudo /usr/local/nginx/sbin/nginx -t # 檢查配置信息是否有錯 $ sudo /usr/local/nginx/sbin/nginx # 啓動nginx,虛擬機要關閉防火牆或開放80端口 $ sudo /usr/local/nginx/sbin/nginx -s stop # 強制中止nginx $ sudo /usr/local/nginx/sbin/nginx -s quit # 優雅關閉nginx $ sudo /usr/local/nginx/sbin/nginx -s reload # 從新加載nginx.conf文件 $ /usr/local/nginx/sbin/nginx -v # 顯示nginx版本信息
方式二:yum
1)先安裝yum-utils
sudo yum install yum-utils
2)添加yum源
vim /etc/yum.repos.d/nginx.repo
添加如下內容
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key
3)安裝nginx
sudo yum install nginx
默認安裝最新的stable版本,之後也能夠經過上面配置的yum源來更新版本
官方文檔:http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@localhost test]# whereis nginx nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
方式三:docker
nginx經常使用命令
命令格式:nginx -s reload
- 幫助:-? -h
- 使用指定的配置文件:-c
- 指定配置指令:-g
- 指定運行目錄:-p
- 發送信號:-s
- 當即中止服務:nginx -s stop
- 優雅中止服務:nginx -s quit
- 重載配置文件:nginx -s reload
- 從新開始記錄日誌文件:nginx -s reopen
- 測試配置文件是否有語法錯誤:-t -T
- 打印nginx版本、編譯信息:-v -V
nginx.conf
本機nginx.conf文件位置的說明
centos 7 使用 yum 安裝的 nginx 默認的安裝目錄以下:
[root@localhost docs]# whereis nginx nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
全部配置文件位於 /etc/nginx/ 下,/etc/nginx/nginx.conf是主配置文件,它使用了include指令引入的其它位置的配置文件。
也就是配置文件有 /etc/nginx/nginx.conf 和 /etc/nginx/conf.d/*.conf
nginx.conf:
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
配置文件結構
配置文件結構,三大部分:
- 主配置塊:從開始到even塊之間的內容
- even塊:主要影響Nginx服務器與用戶的網絡鏈接
- http塊:
- http全局塊
- upstream塊
- server塊
- 全局server塊
- location塊
main block # 主配置段,即全局配置,對http/mail都有效 events { worker_connections 1024; } http { upstream ifacesuportservice_dyexp { } gzip on; server { listen 80; server_name localhost; location /webstatic { gzip off; } } server { listen 81; server_name www.baidu.com; location /webstatic { } } } mail { } stream { }
指令塊由一個塊配置項名和一對大括號組成。經常使用的有http、server、upstream、location。
配置語法
- 配置文件由指令和指令塊構成
- 每條指令以
;
結尾,指令與參數之間以空格分隔 - 指令塊以{}大括號將多條指令組織在一塊兒
- include語句容許組合多個配置文件,以提升能夠維護性
- 使用#符號添加註釋,提升可讀性
- 使用$符號使用變量
- 部分指令的參數支持正則表達式
參數單位
-
時間
- ms,milliseconds
- s,seconds
- m,minutes
- h,hours
- d,days
- w,weeks
- M,months,30d ays
- y,years,365 days
-
空間
- 不寫單位,bytes
- k/K,kilobytes
- m/M,megabytes
- g/G,gigabytes
配置變量
- 內建變量,由nginx模塊提供,可直接引用,官網:http://nginx.org/en/docs/varindex.html
- 自定義變量,用戶使用set命令定義,
set variable_name value
- 引用變量,
$variable_name
全局配置
1)性能優化相關配置
- worker_processes number | auto,worker進程數量,一般爲主機的物理核數
- worker_cpu_affinity
- worker_priority
- worker_rlimit_nofile
2)events事件驅動相關配置
events { worker_connections 1024; accept_mutex on | off; }
accept_mutex on | off 互斥,處理新的鏈接請求
- on指由各個worker輪流處理新請求
- off指每一個新請求的到達都會通知(喚醒)全部進程,但只有一個進程可得到鏈接,形成「驚羣」,影響性能
3)調試和定位問題
daemon on | off;
是否以守護進程運行nginx,默認onmaster_process on | off;
是否以master/worker模型運行nginx,默認onerror_log file [level];
錯誤日誌文件及其級別,出於調試須要,可設定爲debug,但debug僅在編譯時使用了 --with-debug 選項時纔有效,默認:error_log logs/error.log error;
靜態資源web服務器
首先,本機nginx環境:
[root@localhost docs]# whereis nginx nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
1)將tomcat 9 的 docs 上傳到 /usr/share/nginx/tomcat9/
2)配置nginx.conf
最簡單的配置
server { listen 8080; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #root /usr/share/nginx/html; #index index.html index.htm; alias /usr/share/nginx/tomcat9/docs/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
作了兩點修改:
- 監聽8080端口
- 在 location / 塊 使用了 alias,而沒有使用 root
在http塊中,開啓 gzip,這裏的數字僅用於測試,壓縮後文件小了不少:
gzip on; gzip_min_length 1; gzip_comp_level 2; gzip_types text/plain application/xml;
參考官方:http://nginx.org/en/docs/http/ngx_http_gzip_module.html
參考博客:http://www.javashuo.com/article/p-dgsfkyol-ka.html
關於日誌
日誌配置所在的塊結構內的日誌都會記錄在相應的目錄
如上面 server 塊內的 access_log /var/log/nginx/host.access.log main
會記錄全部請求8080端口的日誌
配置多臺虛擬主機
在nginx.conf的http塊下配置多個server塊就是多個虛擬主機
server塊要配置不一樣的server_name以表示主機名,而後須要在hosts文件中作ip與主機之間的映射
http { upstream ifacesuportservice_dyexp { } server { listen 80; server_name localhost; location /webstatic { gzip off; } } server { listen 81; server_name www.baidu.com; location /webstatic { } } }
日誌
1)在http塊中定義日誌格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
2)在各個塊(http、server)中引用日誌格式,並指定日誌位置、日誌文件名稱
access_log /var/log/nginx/access.log main;
日誌配置所在的塊結構內的日誌都會記錄在相應的目錄,也就是各個塊的日誌能夠分開記錄
日誌切分——實現一個定時任務處理nginx日誌
1)編寫一個shell腳本log.sh
,定義如何切分(備份)日誌
#!/bin/sh BASE_DIR=/usr/local/nginx BASE_FILE_NAME=bhz.com.access.log CURRENT_PATH=$BASE_DIR/logs BAK_PATH=$BASE_DIR/datalogs CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME BAK_TIME=`/bin/date -d yesterday +%Y%m%d%H%M` BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME echo $BAK_FILE $BASE_DIR/sbin/nginx -s stop mv $CURRENT_FILE $BAK_FILE $BASE_DIR/sbin/nginx
2)實現定時任務
crontab -e
添加內容以下:
*/1 * * * * * sh /usr/local/nginx/sbin/log.sh
記得修改成本身的文件路徑
location塊
語法:
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: — Context: server, location
官方文檔:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
- location = pattern {} 精準匹配
- location ~ pattern {} 正則匹配,大小寫敏感
- location ~* pattern {} 正則匹配,大小寫不敏感
- location ^~ /path/path {} 不檢查正則表達式
location = /50x.html { root /usr/share/nginx/html; }
題外話:能夠安裝一個【正則表達式終端測試工具】來測試正則表達式:
# yum install -y pcre-tools
如何使用?
- re:輸入正則表達式,//表示開頭和結束
- data:測試的數據
[root@localhost ~]# pcretest PCRE version 8.32 2012-11-30 re> /(\d+)\.(\d+)\.(\d+)\.(\d+)/ data> 192.168.12.133 0: 192.168.12.133 1: 192 2: 168 3: 12 4: 133
rewrite的寫法
官方文檔:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if
flag有
- last 本條規則匹配完成後繼續向下匹配新的location URI規則
- break 本條規則匹配完成後終止,不在匹配任何規則
- redirect 返回302臨時重定向,地址欄改變
- permanent 返回301永久重定向,地址欄改變
參考:https://www.cnblogs.com/brianzhu/p/8624703.html
last與break區別:
總結:
- last,匹配到路徑後,會按照新的路徑請求一次nginx
- break,匹配到路徑後,不會按照新的路徑請求一次nginx,而是會去root目錄尋找文件;若是在location中,則用break,則不是last。