Nginx 配置檔案
預設的config
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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 { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; 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; # } #} }
#
開頭的表示註釋內容,我們去掉所有以 # 開頭的段落,精簡之後的內容如下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
Nginx的配置塊
根據檔案,我們把Nginx的配置檔案分為以下幾塊:
... #全域性塊 events { #events塊 ... } http #http塊 { ... #http全域性塊 server #server塊 { ... #server全域性塊 location [PATTERN] #location塊 { ... } location [PATTERN] { ... } } server { ... } ... #http全域性塊 }
全域性塊
從配置檔案開始到 events 塊之間的內容,主要會設定一些影響nginx 伺服器整體執行的配置指令,主要包括配置執行 Nginx 伺服器的使用者(組)、允許生成的 worker process 數,程序 PID 存放路徑、日誌存放路徑和型別以及配置檔案的引入等。
比如上面第一行配置的:
worker_processes 1;
這是 Nginx 伺服器併發處理服務的關鍵配置,worker_processes 值越大,可以支援的併發處理量也越多,但是會受到硬體、軟體等裝置的制約。
events 塊
比如上面的配置:
events {
worker_connections 1024;
}
events 塊涉及的指令主要影響 Nginx 伺服器與使用者的網路連線,常用的設定包括是否開啟對多 work process 下的網路連線進行序列化,是否允許同時接收多個網路連線,選取哪種事件驅動模型來處理連線請求,每個 word process 可以同時支援的最大連線數等。
上述例子就表示每個 work process 支援的最大連線數為 1024.
這部分的配置對 Nginx 的效能影響較大,在實際中應該靈活配置。
http 塊
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
可以巢狀多個server,配置代理,快取,日誌定義等絕大多數功能和第三方模組的配置。如檔案引入,mime-type定義,日誌自定義,是否使用sendfile傳輸檔案,連線超時時間,單連線請求數等。
需要注意的是:http 塊也可以包括 http全域性塊、server 塊。
http 全域性塊
http全域性塊配置的指令包括檔案引入、MIME-TYPE 定義、日誌自定義、連線超時時間、單鏈接請求數上限等。
server 塊
這塊和虛擬主機有密切關係,虛擬主機從使用者角度看,和一臺獨立的硬體主機是完全一樣的,該技術的產生是為了節省網際網路伺服器硬體成本。後面會詳細介紹虛擬主機的概念。
每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當於一個虛擬主機。
而每個 server 塊也分為全域性 server 塊,以及可以同時包含多個 locaton 塊。
1、全域性 server 塊
最常見的配置是本虛擬機器主機的監聽配置和本虛擬主機的名稱或IP配置。
2、location 塊
一個 server 塊可以配置多個 location 塊。
這塊的主要作用是基於 Nginx 伺服器接收到的請求字串(例如 server_name/uri-string),對虛擬主機名稱(也可以是IP別名)之外的字串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理。地址定向、資料快取和應答控制等功能,還有許多第三方模組的配置也在這裡進行。
配置示例
下面一個配置檔案,作為理解:
########### 每個指令必須有分號結束。#################
#user administrator administrators; #配置使用者或者組,預設為nobody nobody。
#worker_processes 2; #允許生成的程序數,預設為1
#pid /nginx/pid/nginx.pid; #指定nginx程序執行檔案存放地址
error_log log/error.log debug; #制定日誌路徑,級別。這個設定可以放入全域性塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設定網路連線序列化,防止驚群現象發生,預設為on
multi_accept on; #設定一個程序是否同時接受多個網路連線,預設為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連線數,預設為512
}
http {
include mime.types; #副檔名與檔案型別對映表
default_type application/octet-stream; #預設檔案型別,預設為text/plain
#access_log off; #取消服務日誌
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日誌格式的預設值
sendfile on; #允許sendfile方式傳輸檔案,預設為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個程序每次呼叫傳輸數量不能大於設定的值,預設為0,即不設上限。
keepalive_timeout 65; #連線超時時間,預設為75s,可以在http,server,location塊。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連線請求上限次數。
listen 4545; #監聽埠
server_name 127.0.0.1; #監聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
#root path; #根目錄
#index vv.txt; #設定預設頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的伺服器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
上面是nginx的基本配置,需要注意的有以下幾點:
1、自定義格式
-
$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
-
$remote_user :用來記錄客戶端使用者名稱稱;
-
$time_local : 用來記錄訪問時間與時區;
-
$request : 用來記錄請求的url與http協議;
-
$status : 用來記錄請求狀態;成功是200,
-
$body_bytes_s ent :記錄傳送給客戶端檔案主體內容大小;
-
$http_referer :用來記錄從那個頁面連結訪問過來的;
-
$http_user_agent :記錄客戶端瀏覽器的相關資訊;
2、驚群現象:一個網路連線到來,多個睡眠的程序被同事叫醒,但只有一個程序能獲得連結,這樣會影響系統性能。
3、每個指令必須有分號結束。