1. Nginx基本配置
1.1 Nginx預設配置文法

作為主要的配置檔案
nginx.conf
這個包含檔案會把
include /etc/nginx/conf.d/*.conf
目錄下以
conf.d
結尾的字尾配置檔案全部包含到主配置檔案中.
.conf
1.2 全局性和服務級别解釋
user username [groupname]; #指定運作worker程序的使用者群組
pid /path/to/pidfile_name; #指定nginx的pid檔案
worker_rlimit_nofile #num; #指定一個worker程序所能夠打開的最大檔案句柄數;
worker_rlimit_sigpending #num; #設定每個使用者能夠發往worker程序的信号的數量;
#優化性能相關的配置:
worker_processes #; #worker程序的個數;通常其數值應該為CPU的實體核心數減1;
worker_cpu_affinity cpumask ...; #cpu親和性
0000
0001
0010
0100
1000
#worker_processes 6;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000;
ssl_engine device; #在存在ssl硬體加速器的伺服器上,指定所使用的ssl硬體加速裝置;
timer_resolution t; #每次核心事件調用傳回時,都會使用gettimeofday()來更新nginx緩存時鐘;timer_resolution用于定義每隔多久才會由gettimeofday()更新一次緩存時鐘;x86-64系統上,gettimeofday()代價已經很小,可以忽略此配置;
worker_priority nice; #-20,19之間的值;
複制
1.3 Event事件子產品解釋
accept_mutex [on|off] #是否打開Ningx的負載均衡鎖;此鎖能夠讓多個worker進輪流地、序列化地與新的用戶端建立連接配接;而通常當一個worker程序的負載達到其上限的7/8,master就盡可能不再将請求排程此worker;
lock_file /path/to/lock_file; #lock檔案
accept_mutex_delay #ms; #accept鎖模式中,一個worker程序為取得accept鎖的等待時長;如果某worker程序在某次試圖取得鎖時失敗了,至少要等待#ms才能再一次請求鎖;
multi_accept on|off; #是否允許一次性地響應多個使用者請求;預設為Off;
use [epoll|rtsig|select|poll]; #定義使用的事件模型,建議讓nginx自動選擇;
worker_connections #; #每個worker能夠并發響應最大請求數;
複制
1.4 Debug模式解釋
daemon on|off; #是否讓ningx運作背景;預設為on,調試時可以設定為off,使得所有資訊去接輸出控制台;
master_process on|off; #是否以master/worker模式運作nginx;預設為on;調試時可設定off以友善追蹤;
error_log /path/to/error_log level; #錯誤日志檔案及其級别;預設為error級别;調試時可以使用debug級别,但要求在編譯時必須使用--with-debug啟用debug功能;
複制
1.5 Server塊的解釋
#必須使用虛拟機來配置站點;每個虛拟主機使用一個server {}段配置;
server {
}
#非虛拟主機的配置或公共配置,需要定義在server之外,http之内;
http {
directive value;
...
server {
}
server {
}
...
}
複制
1.6 虛拟主機相關配置
server {} #定義一個虛拟主機;nginx支援使用基于主機名或IP的虛拟主機;
listen
listen address[:port];
listen port
# default_server:定義此server為http中預設的server;如果所有的server中沒有任何一個listen使用此參數,那麼第一個server即為預設server;
# rcvbuf=SIZE: 接收緩沖大小;
# sndbuf=SIZE: 發送緩沖大小;
# ssl: https server;
server_name [...];
# server_name可以跟多個主機名,名稱中可以使用通配符和正規表達式(通常以~開頭);當nginx收到一個請求時,會取出其首部的server的值,而後跟衆server_name進行比較;比較方式:
# 1. 先做精确比對;www.magedu.com
# 1. 左側通配符比對;*.magedu.com
# 1. 右側通配符比對;www.abc.com, www.*
# 1. 正規表達式比對: ~^.*\.magedu\.com$
server_name_hash_bucket_size 32|64|128; #為了實作快速主機查找,nginx使用hash表來儲存主機名;
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
# 功能:允許根據使用者請求的URI來比對指定的各location以進行通路配置;比對到時,将被location塊中的配置所處理;比如:http://www.nginx.com/images/logo.gif
# =:精确比對;
# ~:正規表達式模式比對,比對時區分字元大小寫
# ~*:正規表達式模式比對,比對時忽略字元大小寫
# ^~: URI前半部分比對,不檢查正規表達式
# http://www.nginx.com/index.html
# http://www.nginx.com/
# http://www.nginx.com/documents/index.html
# http://www.nginx.com/images/index.html
# http://www.nginx.com/images/a.png
# 比對優先級: 字元字面量最精确比對、正規表達式檢索(由第一個比對到所處理)、按字元字面量
複制
1.7 檔案路徑定義
root path; #設定web資源路徑;用于指定請求的根文檔目錄;
location / {
root /www/htdocs;
}
location ^~ /images/ {
root /web;
}
root: root/URI/
http://www.nginx.com/image/b.html
--> /web/image/b.html
alias path; #隻能用于location中,用于路徑别名;
location / {
root /www/htdocs;
}
location ^~ /images/ {
alias /web/img;
}
alias: alias/
http://www.nginx.com/image/b.html
--> /web/img/b.html
index file ...; #定義預設首頁通路那個頁面,可參跟多個值;
error_page code ... [=[response]] uri; #當對于某個請求傳回錯誤時,如果比對上了error_page指令中設定的code,則重定向到新的URI中。錯誤頁面重定向;
try_files path1 [path2 ...] uri; #自左至右嘗試讀取由path所指定路徑,在第一次找到即停止并傳回;如果所有path均不存在,則傳回最後一個uri;
location ~* ^/documents/(.*)$ {
root /www/htdocs;
try_file $uri /docu/$1 /temp.html;
}
http://www.nginx.com/documents/a.html
http://www.nginx.com/docu/a.html
http://www.nginx.com/temp.html
複制
1.8 網絡連接配接相關設定
keepalive_timeout time; #保持連接配接的逾時時長;預設為75秒;
keepalive_requests n; #在一次長連接配接上允許承載的最大請求數;
keepalive_disable [msie6 | safari | none ]; #對指定的浏覽器禁止使用長連接配接;
tcp_nodelay on|off; #對keepalive連接配接是否使用TCP_NODELAY選項;
client_header_timeout time; #讀取http請求首部的逾時時長;
client_body_timeout time; #讀取http請求包體的逾時時長;
send_timeout time; #發送響應的逾時時長;
複制
1.9 檔案操作優化配置
sendfile on|off; #是否啟用sendfile功能;
aio on|off; #是否啟用aio功能;
open_file_cache max=N [inactive=time]|off; #是否打開檔案緩存功能;
# max: 緩存條目的最大值;當滿了以後将根據LRU算法進行置換;
# inactive: 某緩存條目在指定時長時沒有被通路過時,将自動被删除;預設為60s;
# 緩存的資訊包括:
檔案句柄、檔案大小和上次修改時間;
已經打開的目錄結構;
沒有找到或沒有通路權限的資訊;
open_file_cache_errors on|off; #是否緩存檔案找不到或沒有權限通路等相關資訊;
open_file_cache_valid time; #多長時間檢查一次緩存中的條目是否超出非活動時長,預設為60s;
open_file_cache_min_use #; #在inactive指定的時長内被通路超此處指定的次數地,才不會被删除;
複制
1.10 記憶體及磁盤資源配置設定:
client_body_in_file_only on|clean|off; #HTTP的包體是否存儲在磁盤檔案中;非off表示存儲,即使包體大小為0也會建立一個磁盤檔案;on表示請求結束後包體檔案不會被删除,clean表示會被删除;
client_body_in_single_buffer on|off; #HTTP的包體是否存儲在記憶體buffer當中;預設為off;
cleint_body_buffer_size size; #nginx接收HTTP包體的記憶體緩沖區大小;
client_body_temp_path dir-path [level1 [level2 [level3]]]; # HTTP包體存放的臨時目錄;
# client_body_temp_path /var/tmp/client/ 1 2
client_header_buffer_size size; #正常情況下接收使用者請求的http封包header部分時配置設定的buffer大小;預設為1k;
large_client_header_buffers number size; #存儲超大Http請求首部的記憶體buffer大小及個數;
connection_pool_size size; #nginx對于每個建立成功的tcp連接配接都會預先配置設定一個記憶體池,此處即用于設定此記憶體池的初始大小;預設為256;
request_pool_size size; #nginx在處理每個http請求時會預先配置設定一個記憶體池,此處即用于設定此記憶體池的初始大小;預設為4k;
複制
2. HTTP請求
curl http://www.baidu.com 傳回請求内容
curl -v http://www.baidu.com >/dev/null 重定向空裝置可檢視隐藏頭資訊
#request資訊
> GET / HTTP/1.1 請求協定
> User-Agent: curl/7.29.0 請求裝置
> Host: www.baidu.com 請求位址
> Accept: */* 請求的類型,這裡是任意類型
#response資訊
< HTTP/1.1 200 OK 200服務端正常相應
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Sun, 24 Mar 2019 04:05:06 GMT
< Etag: "588604cf-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:43 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
複制
3. Nginx日志類型
error.log 記錄處理http請求的錯誤狀态以及nginx本身服務的錯誤狀态
access_log 每次http請求的通路狀态
3.1 log_format文法
-
依賴log_format
ngx_http_log_module
#log_format文法
Syntax: log_format name [escape=default|json|none] string ...;
Default:
log_format combined "...";
Context: http
複制
- access_log 配置格式
#access_log輸出預覽
192.168.0.106 - - [24/Mar/2019:14:54:48 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-"
複制
3.2 Nginx變量
#access.log中輸出user-agent值
log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#日志輸出類型預覽
curl/7.29.0127.0.0.1 - - [24/Mar/2019:15:08:36 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
複制
Nginx内置變量官方:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_status
3.3 log_format變量解釋
- log_format main日志變量的解釋
$remote_addr 表示用戶端的位址
$remote_user 表示用戶端請求nginx認證的使用者名
$time_local 表示時間
$request 表示request請求頭
$status 表示response的傳回狀态
$body_bytes_sent 表示傳回資料的大小
$http_referer 表示上級頁面的位址
$http_user_agent 表示user用戶端的資訊
$http_x_forwarded_for 表示每一級請求攜帶的資訊
複制
nginx内置變量中文文檔:https://www.i7dom.cn/235/2020/17/nginx-doc01.html
版權屬于:龍之介大人