概述
Nginx 是使用一個 master 進程來管理多個 worker 進程提供服務。master 負責管理 worker 進程,而 worker 進程則提供真正的客戶服務,worker 進程的數量一般跟服務器上 CPU 的核心數相同,worker 之間通過一些進程間通信機制實現負載均衡等功能。Nginx 進程之間的關係可由下圖表示:
Nginx 服務啓動時會讀入配置文件,後續的行爲則按照配置文件中的指令進行。Nginx 的配置文件是純文本文件,默認安裝 Nginx 後,其配置文件均在 /usr/local/nginx/conf/ 目錄下。其中,nginx.conf 爲主配置文件。配置文件中以 # 開始的行,或者是前面有若干空格或者 TAB 鍵,然後再跟 # 的行,都被認爲是註釋。這裏只是瞭解主配置文件的結構。
Nginx 配置文件是以 block(塊)形式組織,每個 block 都是以一個塊名字和一對大括號 “{}” 表示組成,block 分爲幾個層級,整個配置文件爲 main 層級,即最大的層級;在 main 層級下可以有 event、http 、mail 等層級,而 http 中又會有 server block,server block中可以包含 location block。即塊之間是可以嵌套的,內層塊繼承外層塊。最基本的配置項語法格式是“配置項名 配置項值1 配置項值2 配置項值3 ... ”;
每個層級可以有自己的指令(Directive),例如 worker_processes 是一個main層級指令,它指定 Nginx 服務的 Worker 進程數量。有的指令只能在一個層級中配置,如worker_processes 只能存在於 main 中,而有的指令可以存在於多個層級,在這種情況下,子 block 會繼承 父 block 的配置,同時如果子block配置了與父block不同的指令,則會覆蓋掉父 block 的配置。指令的格式是“指令名 參數1 參數2 … 參數N;”,注意參數間可用任意數量空格分隔,最後要加分號。
下圖是 Nginx 配置文件通常結構圖示。
Nginx 服務的基本配置項
Nginx 服務運行時,需要加載幾個核心模塊和一個事件模塊,這些模塊運行時所支持的配置項稱爲基本配置;基本配置項大概可分爲以下四類:
- 用於調試、定位的配置項;
- 正常運行的必備配置項;
- 優化性能的配置項;
- 事件類配置項;
各個配置項的具體實現如下:
/* Nginx 服務基本配置項 */
/* 用於調試、定位的配置項 */
#以守護進程 Nginx 運行方式
#語法:daemon off | on;
#默認:daemon on;
#master / worker 工作方式
#語法:master_process on | off;
#默認:master_process on;
#error 日誌設置
# 路徑 錯誤級別
#語法:error_log /path/file level;
#默認:error_log logs/error.log error;
#其中/path/file是一個具體文件;level是日誌的輸出級別,其取值如下:
# debug info notice warn error crit alert emerg
#從左至右級別增大;若設定一個級別後,則在輸出的日誌文件中只輸出級別大於或等於已設定的級別;
#處理特殊調試點
#語法:debug_points [stop | abort]
#這個設置是來跟蹤調試 Nginx 的;
#僅對指定的客戶端輸出 debug 級別的日誌
#語法:debug_connection [IP | DIR]
#限制 coredump 核心轉儲文件的大小
#語法:worker_rlimit_core size;
#指定 coredump 文件的生成目錄
#語法:working_directory path;
/* 正常運行的配置項 */
#定義環境變量
#語法:env VAR | VAR=VALUE;
#VAR 是變量名,VALUE 是目錄;
#嵌入其他配置文件
#語法:include /path/file;
#include 配置項可以將其他配置文件嵌入到 Nginx 的 nginx.conf 文件中;
#pid 的文件路徑
#語法:pid path/file;
#默認:pid logs/nginx.pid;
#保存 master 進程 ID 的 pid 文件存放路徑;
#Nginx worker 運行的用戶及用戶組
#語法:user username [groupname];
#默認:user nobody nobody;
#指定 Nginx worker進程可打開最大句柄個數
#語法:worker_rlimit_nofile limit;
#限制信號隊列
#語法:worker_rlimit_sigpending limit;
#設置每個用戶發給 Nginx 的信號隊列大小,超出則丟棄;
/* 優化性能配置項 */
#Nginx worker 進程的個數
#語法:worker_process number;
#默認:worker_process 1;
#綁定 Nginx worker 進程到指定的 CPU 內核
#語法:worker_cpu_affinity cpumask [cpumask...]
#SSL 硬件加速
#語法:ssl_engine device;
#系統調用 gettimeofday 的執行頻率
#語法:timer_resolution t;
#Nginx worker 進程優先級設置
#語法:worker_priority nice;
#默認:worker_priority 0;
/* 事件類配置項 */
#一般有以下幾種配置:
#1、是否打開accept鎖
# 語法格式:accept_mutex [on | off];
#2、lock文件的路徑
# 語法格式:lock_file path/file;
#3、使用accept鎖後到真正建立連接之間的延遲時間
# 語法格式:accept_mutex_delay Nms;
#4、批量建立新連接
# 語法格式:multi_accept [on | off];
#
#5、選擇事件模型
# 語法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
#6、每個worker進行的最大連接數
# 語法格式:worker_connections number;
HTTP 核心模塊的配置
具體可以參看《Nginx 中 HTTP 核心模塊配置》
/* HTTP 核心模塊配置的功能 */
/* 虛擬主機與請求分發 */
#監聽端口
#語法:listen address:port[default | default_server | [backlong=num | rcvbuf=size | sndbuf=size |
# accept_filter | deferred | bind | ipv6only=[on | off] | ssl]];
# 默認:listen:80;
# 說明:
# default或default_server:將所在的server塊作爲web服務的默認server塊;當請求無法匹配配置文件中的所有主機名時,就會選擇默認的虛擬主機;
# backlog=num:表示 TCP 中backlog隊列存放TCP新連接請求的大小,默認是-1,表示不予設置;
# rcvbuf=size:設置監聽句柄SO_RCVBUF的參數;
# sndbuf=size:設置監聽句柄SO_SNDBUF的參數;
# accept_filter:設置accept過濾器,只對FreeBSD操作系統有用;
# deferred:設置該參數後,若用戶發起TCP連接請求,並且完成TCP三次握手,但是若用戶沒有發送數據,則不會喚醒worker進程,直到發送數據;
# bind:綁定當前端口 / 地址對,只有同時對一個端口監聽多個地址時纔會生效;
# ssl:在當前端口建立的連接必須基於ssl協議;
#配置塊範圍:server
#主機名稱
#語法:server_name name[...];
#默認:server_name "";
#配置塊範圍:server
#server name 是使用散列表存儲的
#每個散列桶佔用內存大小
#語法:server_names_hash_bucket_size size;
#默認:server_names_hash_bucker_size 32|64|128;
#
#散列表最大bucket數量
#語法:server_names_hash_max_size size;
#默認:server_names_hash_max_size 512;
#默認:server_name_in_redirect on;
#配置塊範圍:server、http、location
#處理重定向主機名
#語法:server_name_in_redirect on | off;
#默認:server_name_in_redirect on;
#配置塊範圍:server、http、location
#location語法:location[= | ~ | ~* | ^~ | @] /uri/ {}
#配置塊範圍:server
#location嘗試根據用戶請求中的URI來匹配 /uri表達式,若匹配成功,則執行{}裏面的配置來處理用戶請求
#以下是location的一般配置項
#1、以root方式設置資源路徑
# 語法格式:root path;
#2、以alias方式設置資源路徑
# 語法格式:alias path;
#3、訪問首頁
# 語法格式:index file...;
#4、根據HTTP返回碼重定向頁面
# 語法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
#5、是否允許遞歸使用error_page
# 語法格式:recursive_error_pages [on | off];
#6、try_files
# 語法格式:try_files path1 [path2] uri;
/* 文件路徑的定義 */
#root方式設置資源路徑
#語法:root path;
#默認:root html;
#配置塊範圍:server、http、location、if
#以alias方式設置資源路徑
#語法:alias path;
#配置塊範圍:location
#訪問主頁
#語法:index file...;
#默認:index index.html;
#配置塊範圍:http、server、location
#根據HTTP返回碼重定向頁面
# 語法:error_page code [code...] [= | =answer-code] uri | @named_location;
#配置塊範圍:server、http、location、if
#是否允許遞歸使用error_page
# 語法:recursive_error_pages [on | off];
#配置塊範圍:http、server、location
#try_files
# 語法:try_files path1 [path2] uri;
#配置塊範圍:server、location
/* 內存及磁盤資源分配 */
# HTTP 包體只存儲在磁盤文件中
# 語法:client_body_in_file_only on | clean | off;
# 默認:client_body_in_file_only off;
# 配置塊範圍:http、server、location
# HTTP 包體儘量寫入到一個內存buffer中
# 語法:client_body_single_buffer on | off;
# 默認:client_body_single_buffer off;
# 配置塊範圍:http、server、location
# 存儲 HTTP 頭部的內存buffer大小
# 語法:client_header_buffer_size size;
# 默認:client_header_buffer_size 1k;
# 配置塊範圍:http、server
# 存儲超大 HTTP 頭部的內存buffer大小
# 語法:large_client_header_buffer_size number size;
# 默認:large_client_header_buffer_size 4 8k;
# 配置塊範圍:http、server
# 存儲 HTTP 包體的內存buffer大小
# 語法:client_body_buffer_size size;
# 默認:client_body_buffer_size 8k/16k;
# 配置塊範圍:http、server、location
# HTTP 包體的臨時存放目錄
# 語法:client_body_temp_path dir-path [level1 [level2 [level3]]];
# 默認:client_body_temp_path client_body_temp;
# 配置塊範圍:http、server、location
# 存儲 TCP 成功建立連接的內存池大小
# 語法:connection_pool_size size;
# 默認:connection_pool_size 256;
# 配置塊範圍:http、server
# 存儲 TCP 請求連接的內存池大小
# 語法:request_pool_size size;
# 默認:request_pool_size 4k;
# 配置塊範圍:http、server
/* 網絡連接設置 */
# 讀取 HTTP 頭部的超時時間
# 語法:client_header_timeout time;
# 默認:client_header_timeout 60;
# 配置塊範圍:http、server、location
# 讀取 HTTP 包體的超時時間
# 語法:client_body_timeout time;
# 默認:client_body_timeout 60;
# 配置塊範圍:http、server、location
# 發送響應的超時時間
# 語法:send_timeout time;
# 默認:send_timeout 60;
# 配置塊範圍:http、server、location
# TCP 連接的超時重置
# 語法:reset_timeout_connection on | off;
# 默認:reset_timeout_connection off;
# 配置塊範圍:http、server、location
# 控制關閉 TCP 連接的方式
# 語法:lingering_close off | on | always;
# 默認:lingering_close on;
# 配置塊範圍:http、server、location
# always 表示關閉連接之前無條件處理連接上所有用戶數據;
# off 表示不處理;on 一般會處理;
# lingering_time
# 語法:lingering_time time;
# 默認:lingering_time 30s;
# 配置塊範圍:http、server、location
# lingering_timeout
# 語法:lingering_timeout time;
# 默認:lingering_time 5s;
# 配置塊範圍:http、server、location
# 對某些瀏覽器禁止keepalive功能
# 語法:keepalive_disable [mise6 | safari | none]...
# 默認:keepalive_disable mise6 safari;
# 配置塊範圍:http、server、location
# keepalive超時時間
# 語法:keepalive_timeout time;
# 默認:keepalive_timeout 75;
# 配置塊範圍:http、server、location
# keepalive長連接上允許最大請求數
# 語法:keepalive_requests n;
# 默認:keepalive_requests 100;
# 配置塊範圍:http、server、location
# tcp_nodelay
# 語法:tcp_nodelay on | off;
# 默認:tcp_nodelay on;
# 配置塊範圍:http、server、location
# tcp_nopush
# 語法:tcp_nopush on | off;
# 默認:tcp_nopush off;
# 配置塊範圍:http、server、location
/* MIME 類型設置 */
# MIME type 與文件擴展的映射
# 語法:type{...}
# 配置塊範圍:http、server、location
# 多個擴展名可映射到同一個 MIME type
# 默認 MIME type
# 語法:default_type MIME-type;
# 默認:default_type text/plain;
# 配置塊範圍:http、server、location
# type_hash_bucket_size
# 語法:type_hash_bucket_size size;
# 默認:type_hash_bucket_size 32 | 64 | 128;
# 配置塊範圍:http、server、location
# type_hash_max_size
# 語法:type_hash_max_size size;
# 默認:type_hash_max_size 1024;
# 配置塊範圍:http、server、location
/* 限制客戶端請求 */
# 按 HTTP 方法名限制用戶請求
# 語法:limit_except method...{...}
# 配置塊:location
# method 的取值如下:
# GET、HEAD、POST、PUT、DELETE、MKCOL、COPY、MOVE、OPTIONS、
# PROPFIND、PROPPATCH、LOCK、UNLOCK、PATCH
# HTTP 請求包體的最大值
# 語法:client_max_body_size size;
# 默認:client_max_body_size 1m;
# 配置塊範圍:http、server、location
# 對請求限制速度
# 語法:limit_rate speed;
# 默認:limit_rate 0;
# 配置塊範圍:http、server、location、if
# 0 表示不限速
# limit_rate_after規定時間後限速
# 語法:limit_rate_after time;
# 默認:limit_rate_after 1m;
# 配置塊範圍:http、server、location、if
/* 文件操作的優化 */
# sendfile系統調用
# 語法:sendfile on | off;
# 默認:sendfile off;
# 配置塊:http、server、location
# AIO 系統調用
# 語法:aio on | off;
# 默認:aio off;
# 配置塊:http、server、location
# directio
# 語法:directio size | off;
# 默認:directio off;
# 配置塊:http、server、location
# directio_alignment
# 語法:directio_alignment size;
# 默認:directio_alignment 512;
# 配置塊:http、server、location
# 打開文件緩存
# 語法:open_file_cache max=N [inactive=time] | off;
# 默認:open_file_cache off;
# 配置塊:http、server、location
# 是否緩存打開文件的錯誤信息
# 語法:open_file_cache_errors on | off;
# 默認:open_file_cache_errors off;
# 配置塊:http、server、location
# 不被淘汰的最小訪問次數
# 語法:open_file_cache_min_user number;
# 默認:open_file_cache_min_user 1;
# 配置塊:http、server、location
# 檢驗緩存中元素有效性的頻率
# 語法:open_file_cache_valid time;
# 默認:open_file_cache_valid 60s;
# 配置塊:http、server、location
/* 客戶請求的特殊處理 */
# 忽略不合法的 HTTP 頭部
# 語法:ignore_invalid_headers on | off;
# 默認:ignore_invalid_headers on;
# 配置塊:http、server
# HTTP 頭部是否允許下劃線
# 語法:underscores_in_headers on | off;
# 默認:underscores_in_headers off;
# 配置塊:http、server
# If_Modified_Since 頭部的處理策略
# 語法:if_modified_since [off | exact | before]
# 默認:if_modified_since exact;
# 配置塊:http、server、location
# 文件未找到時是否記錄到error日誌
# 語法:log_not_found on | off;
# 默認:log_not_found on;
# 配置塊:http、server、location
# 是否合併相鄰的“/”
# 語法:merge_slashes on | off;
# 默認:merge_slashes on;
# 配置塊:http、server、location
# DNS解析地址
# 語法:resolver address...;
# 配置塊:http、server、location
# DNS解析的超時時間
# 語法:resolver_timeout time;
# 默認:resolver_timeout 30s;
# 配置塊:http、server、location
# 返回錯誤頁面是否在server中註明Nginx版本
# 語法:server_tokens on | off;
# 默認:server_tokens on;
# 配置塊:http、server、location
以下是在 Ubuntu 12.04 系統成功安裝 Nginx 之後的主配置文件:
#Nginx服務器正常啓動時會讀取該配置文件,以下的值都是默認的,若需要可自行修改;
#以下是配置選項
#Nginx worker進程運行的用戶以及用戶組
#語法格式:user username[groupname]
#user nobody;
#Nginx worker 進程個數
worker_processes 1;
#error 日誌設置
#語法格式:error /path/file level
#其中/path/file是一個具體文件;level是日誌的輸出級別,其取值如下:
#debug info notice warn error crit alert emerg,從左至右級別增大;
#若設定一個級別後,則在輸出的日誌文件中只輸出級別大於或等於已設定的級別;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#保存master進程ID的pid文件存放路徑
#語法格式:pid path/file
#pid logs/nginx.pid;
#事件類配置項
#一般有以下幾種配置:
#1、是否打開accept鎖
# 語法格式:accept_mutex [on | off];
#2、lock文件的路徑
# 語法格式:lock_file path/file;
#3、使用accept鎖後到真正建立連接之間的延遲時間
# 語法格式:accept_mutex_delay Nms;
#4、批量建立新連接
# 語法格式:multi_accept [on | off];
#5、選擇事件模型
# 語法格式:use [kqueue | rtisg | epoll | /dev/poll | select | poll | eventport];
#6、每個worker進行的最大連接數
# 語法格式:worker_connections number;
events {
worker_connections 1024;
}
#以下是http模塊
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#server塊
# 每個server塊就是一個虛擬主機,按照server_name來區分
server {
#監聽端口
listen 80;
#主機名稱
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location語法:location[= | ~ | ~* | ^~ | @] /uri/ {}
#location嘗試根據用戶請求中的URI來匹配 /uri表達式,若匹配成功,則執行{}裏面的配置來處理用戶請求
#以下是location的一般配置項
#1、以root方式設置資源路徑
# 語法格式:root path;
#2、以alias方式設置資源路徑
# 語法格式:alias path;
#3、訪問首頁
# 語法格式:index file...;
#4、根據HTTP返回碼重定向頁面
# 語法格式:error_page code [code...] [= | =answer-code] uri | @named_location;
#5、是否允許遞歸使用error_page
# 語法格式:recursive_error_pages [on | off];
#6、try_files
# 語法格式:try_files path1 [path2] uri;
location / {
root html;
index index.html index.htm;
}
#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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
參考資料:
《深入理解Nginx》
https://www.kancloud.cn/digest/understandingnginx/202587
原文出處:https://www.cnblogs.com/pm0101/p/11804276.html