Nginx的全部配置文件都在 /etc/nginx 目錄下面nginx.conf
html
主要有main配置段 ,也叫核心配置段,主配置段
node
還有http和mail
nginx
配置指令以分號結尾(;)並且語法區分大小寫web
eg: directive value1 [value2 ...];正則表達式
並且還支持使用變量瀏覽器
模塊內置變量 :變量自動賦值 緩存
自定義變量:set var_name value
服務器
主配置段的指令的類別:
網絡
用於調試,定位問題(生產中不用開啓)
併發
正常運行,必備的配置
優化性能的配置
事件相關的配置(一個事件響應多個請求,master指向root,worker 指向的Nginx。)
正常運行的必備配置:
一、user USERNAME [GROUPNAME];
指定運行worker進程的用戶 和組,例如:
user nginx nginx;s
二、pid /path/to/pid_file;
指定nginx的pid文件;
三、worker_rlimit_nofile #;
指定一個worker進程所可以打開的最大文件句柄數;
四、worker_rlimit_sigpending #;
指定每一個用戶可以發往worker的信號的數量;
優化性能相關的配置:
一、worker_processes #:
worker線程的個數;一般應該爲物理CPU核心個數減1;
啓動的work線程的個數。
二、worker_cpu_affinity cpumask ...;
綁定worker進程至指定的CPU上;
CPUMASK
0001
0010
0100
1000
例如:
worker_cpu_affinity 00000001 00000010 00000100;
三、timer_resolution t;
gettimeofday();
四、worker_priority nice;
-20, 19
事件相關的配置:
一、accept_mutex [on|off]
內部調用用戶 請求至各worker時用的負載均衡鎖;打開時表示能讓多個worker輪流 地、序列化地與響應新請求;
二、lock_file /path/to/lock_file;
三、accept_mutex_delay #ms;
四、use [epoll|rgsig|select|poll];
定義使用的事件模型;建議讓Nginx自動選擇;
五、worker_connections #;
每一個worker進程所可以響應的最大併發請求數;
用於調試、定位問題:
一、daemon [off|on]
是否以守護進程方式啓動nginx;
二、master_process on|off;
是否以master/worker模型來運行nginx;
三、error_log /path/to/error_log level;
錯誤日誌文件及其級別;出於調試的目的,能夠使用debug級別,但此級別只有在編譯 nginx時使用了--with-debug選項纔有效;
Nginx
main配置段 http { } http配置:http core 配置一個靜態web服務器 ngx_http_core_module 配置框架: http { upstream { ... } server { listen IP:PORT; # 虛擬主機 location /URL { if ...{ ... } root "/path/to/somewhere"; ... } } server { ,,. } } 注意:與http配置相關的指令必須放在http、server、location、upstream、if塊中; 虛擬主機相關的配置: 一、server {} 定義一個虛擬主機; 二、listen 監聽的端口 完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]]; listen address[:port] [default_server] ssl backlog=number: 指明TCP協議backlog隊列的大小。默認爲-1,表示不設置; rcvbuf=size:設定監聽句柄的SO_RCVBUF參數; 例如: listen 172.16.100.8:8080 三、server_name name [...]; 後可跟多個主機名;名稱還能夠使用通配符和正則表達式(~); (1) 先作精確匹配;www.magedu.com: (2) 左側通配符匹配,例如:*.magedu.com; (3) 右側通配符匹配,例如:www.*; (4) 正則表達式匹配,例如: ~^.*\.magedu\.com$ (5) default_server 四、location [=|~|~*|^~] /uri {...} location @name 功能:容許根據用戶請求的URI來匹配定義的各location,匹配到時,此請求將被相應 的location塊中的配置所處理; =: 精確匹配檢查; ~: 正則表達式模式匹配,區分字符大小寫; ~*:正則表達式模式 匹配,不區分字符大小寫; ^~:URI的前半部分匹配,不檢查正則表達式; 匹配優先級:精確匹配(=)、^~、~和~*、由不帶符號的URL進行左側匹配; 五、root 設置web資源路徑映射;用於指明請求的URL所對應的文檔的根目錄路徑; location /p_w_picpaths/ { root "/web/imgs/"; } 六、alias path 用於location配置段,定義路徑別名 location /p_w_picpaths/ { alias /www/pictures/; } 注意:root表示指明路徑爲對應location的「 /」 URL;alias表示路徑映射,即 location中的URL是相對於alias所指明的路徑而言; 七、index file 默認主頁面 index index.html; 八、error_page code [...] [=code] URI | @name 根據http狀態碼重定向錯誤頁面 error_page 404 /404.html =[code]: 以指定的響應碼進行響應;省略code表示以新資源的響應碼爲響應碼; 九、try_files try_files path1[,path2,...] URI 網絡鏈接相關的配置: 一、keepalive_timeout time; 保持鏈接的超時時長,默認爲75s; 二、keepalive_requests #; 在一次保持鏈接上容許承載最大資源請求數; 三、keepalive_disable [msie6|safari|none] 爲指定類型的瀏覽器禁用長鏈接; 四、tcp_nodelay on|off 對長鏈接是否使用TCP_NODELAY選項; 五、client_header_timeout time; 讀取http請求報文首部的超時時長; 六、client_body_timeout time; 讀取http請求報文body部分的超時時長; 七、send_timeout time; 發送響應報文的超時時長; 對客戶端請求進行限制: 一、limit_except METHOD {...} 指定對範圍以外的其它方法的訪問控制; limit_except GET { allow 172.16.0.0/16; deny all; } 二、client_body_max_size SIZE; 限制請求報文中body部分的上限;經過檢測請求報文首部中的"Content_Length"來判 定; 三、limit_rate speed; 限制客戶端每秒種傳輸的字節數,默認爲0,表示無限制; 對內存或磁盤資源進行分配 一、client_body_in_file_only on|clean|off; 請求報文的body部分是否可暫存於磁盤;on表示容許,而且即便請求結束,也不會刪 除暫存的內容;clean表示會刪除;off不容許暫存; 二、client_body_in_single_buffer on|off 三、client_body_buffer_size size; 四、client_body_temp_path DIR [level1 [level2 [level3 [level4]]]] 例如:client_body_temp_path /var/tmp/nginx/client 1 2 五、client_header_buffer_size size: MIME類型相關的配置: 一、types {} 定義MIME types至文件的擴展名; types { text/html .html; p_w_picpath/jpeg .jpg; } 二、default_type MIME-TYPE; 文件操做優化相關的配置: 一、sendfile on|off; 二、aio on|off; 三、directio size|off; 是否使用O_DIRECT選項去請求讀取文件;與sendfile功能互斥; 四、open_file_cache max=N[inactive=time] | off; nginx能夠緩存如下三種信息: (1) 文件句柄、文件大小和最近一次修改時間; (2) 打開目錄的目錄結構; (3) 沒有找到的或者沒有權限操做的文件的相關信息; max=N表示可緩存的最大條目上限;一旦到達上限,則會使用LRU從緩存中刪除最近最 少使用的條目; inactive=time: 在inactive指定的時長內沒有被訪問過的緩存條目就會淘汰; 五、open_file_cache_errors on|off; 是否緩存在文件緩存中緩存打開文件時出現找不到路徑,沒有權限等的錯誤信息; 六、open_file_cache_min_uses time; 每隔多久檢查一次緩存中緩存條目的有效性;默認60s; 重點關注:server{}, location{}, listen, server_name, root, alias, keepalive_timeout, keepalive_requests, error_page 基於IP的訪問控制: http, server, location allow, deny 基於用戶的basic認證配置: auth_basic auth_basic_user_file htpasswd命令建立用戶帳號文件; 基於gzip實現響應報文壓縮: 定製響應首部 add_header name value [always]; expires 定製訪問日誌 log_format access_log 定製出與httpd的combined格式相同的日誌內容; 定義合法引用: valid_referers none | blocked | server_names | string ...; if ($invalid_referer) { return 403 } URL rewrite: URL地址重寫 rewrite regex replacement [flag]; last break redirect permanent if (condition) { ... }