nginx檔案結構與解析,例子,nginx解析
1.nginx檔案結構
... #全域塊events { #events塊 ...}http #http塊{ ... #http全域塊 server #server塊 { ... #server全域塊 location [PATTERN] #location塊 { ... } location [PATTERN] { ... } } server { ... } ... #http全域塊}View Code
2.解析
1、全域塊:配置影響nginx全域的指令。一般有運行nginx伺服器的使用者組,nginx進程pid存放路徑,日誌存放路徑,設定檔引入,允許產生worker process數等。
2、events塊:配置影響nginx伺服器或與使用者的網路連接。有每個進程的最大串連數,選取哪種事件驅動模型處理串連請求,是否允許同時接受多個網路串連,開啟多個網路連接序列化等。
3、http塊:可以嵌套多個server,配置代理,緩衝,記錄定義等絕大多數功能和第三方模組的配置。如檔案引入,mime-type定義,日誌自訂,是否使用sendfile傳輸檔案,連線逾時時間,單串連請求數等。
4、server塊:配置虛擬機器主機的相關參數,一個http中可以有多個server。
5、location塊:配置請求的路由,以及各種頁面的處理情況。
3.舉個栗子
########### 每個指令必須有分號結束。##################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|emergevents { 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; #錯誤頁
proxy_intercept_errors on; #如果被Proxy 伺服器返回的狀態代碼為400或者大於400,設定的error_page配置起作用。預設為off。
proxy_method get; #支援用戶端的要求方法。post/get;
proxy_http_version 1.0 ; #Nginx伺服器提供代理服務的http協議版本1.0,1.1,預設設定為1.0版本
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、1.$remote_addr 與$http_x_forwarded_for 用以記錄用戶端的ip地址; 2.$remote_user :用來記錄用戶端使用者名稱稱; 3.$time_local : 用來記錄訪問時間與時區;4.$request : 用來記錄請求的url與http協議;
5.$status : 用來記錄請求狀態;成功是200, 6.$body_bytes_s ent :記錄發送給用戶端檔案主體內容大小;7.$http_referer :用來記錄從那個頁面連結訪問過來的; 8.$http_user_agent :記錄用戶端瀏覽器的相關資訊;
2、驚群現象:一個網路串連到來,多個睡眠的進程被同事叫醒,但只有一個進程能獲得連結,這樣會影響系統效能。
3、每個指令必須有分號結束。
原文摘抄自:http://www.cnblogs.com/knowledgesea/p/5175711.html
nginx進階:http://www.cnblogs.com/knowledgesea/p/5199046.html