分類:
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
nginx.conf文件在安裝目錄/conf目錄下
1、定義Nginx運行的用戶和用戶組
user nginx nginx;
2、nginx進程數,建議設置為等於CPU總核心數
worker_processes 1;
3、全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
4、進程文件
pid /var/run/nginx.pid;
5、工作模式與連接數上限:worker_connections是單個后台worker process進程的最大並發鏈接數,並發總數是 worker_processes 和 worker_connections 的乘積, 即 max_clients = worker_processes * worker_connections
events { worker_connections 1024; }
6、http下的一些配置及其意義
include mime.types; #文件擴展名與文件類型映射表 default_type application/octet-stream; #默認文件類型 sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來 輸出文件,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置 為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常 把這個改成off。 autoindex on; #開啟目錄列表訪問,合適下載服務器,默認關閉。 tcp_nopush on; #防止網絡阻塞 tcp_nodelay on; #防止網絡阻塞 keepalive_timeout 120; #長連接超時時間,單位是秒 gzip on; #開啟gzip壓縮輸出
7、server虛擬主機一些配置及其意義
例如:
http{
#虛擬主機1 server{ listen 80; server_name www.nginx1.com; location / { root html; index index.html index.htm; } } #虛擬主機2 server{ listen 80; server_name localhost; location / { root html; index index.html index.htm; } } }
這里server_name配置域名的時候,如果是本地測試,需要到windos下hosts文件里,把你的域名和ip添加進去(C:\Windows\System32\drivers\etc\hosts)
nginx支持三種類型的 虛擬主機配置
- 1、基於ip的虛擬主機, (一塊主機綁定多個ip地址)
server{ listen 192.168.1.1:80; server_name localhost; } server{ listen 192.168.1.2:80; server_name localhost; }
- 2、基於域名的虛擬主機(servername)
#域名可以有多個,用空格隔開 server{ listen 80; server_name www.nginx1.com www.nginx2.com; } server{ listen 80; server_name www.nginx3.com; }
- 3、基於端口的虛擬主機(listen不寫ip的端口模式)
server{ listen 80; server_name localhost; } server{ listen 81; server_name localhost; }
server下的location映射解析(官方中文文檔:ngx_http_core_module)匹配規則:location [ = | ~ | ~* | ^~ ] uri { ... }
location URI {}:
對當前路徑及子路徑下的所有對象都生效;
location = URI {}:
精確匹配指定的路徑(注意URL最好為具體路徑),不包括子路徑,因此,只對當前資源生效;
location ~ URI {}:
location ~* URI {}:
模式匹配URI,此處的URI可使用正則表達式,~區分字符大小寫,~*不區 分字符大小寫;
location ^~ URI {}:
不再檢查正則表達式
優先級:= > ^~ > ~|~* > /|/dir/
舉例:
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } 解答:請求“/”匹配配置A, 請求“/index.html”匹配配置B, 請求“/documents/document.html”匹配配置C, 請求“/images/1.gif”匹配配置D, 請求“/documents/1.jpg”匹配配置E
location配置規則
1、“ =”前綴的指令嚴格匹配這個查詢。如果找到,停止搜索。
2、所有剩下的常規字符串,匹配最精確的(一般最長的那個)。如果這個匹配使用^〜前綴,搜索停止。
3、正則表達式,在配置文件中是從上往下匹配的
4、如果第3條規則產生匹配的話,結果被使用。否則,如同從第2條規則被使用
特殊情況:
兩種情況下,不需要繼續匹配正則 location :
( 1 ) 當普通 location 前面指定了“ ^~ ”,特別告訴 Nginx 本條普 通 location 一旦匹配上,則不需要繼續正則匹配。
( 2 ) 當普通location 恰好嚴格匹配上 ,不是最大前綴匹配,則不再繼續匹配正則
另外nginx的反向代理、Tengine(Nginx的升級版)的健康檢查 也用到了location知識,可以去看看
