##代碼塊中的events、http、server、location、upstream等都是塊配置項##
##塊配置項可以嵌套。內層塊直接繼承外層快,例如:server塊裏的任意配置都是基於http塊裏的已有配置的##
##Nginx worker進程運行的用戶及用戶組
#語法:user username[groupname] 默認:user nobody nobody
#user用於設置master進程啓動後,fork出的worker進程運行在那個用戶和用戶組下。當按照"user username;"設置時,用戶組名與用戶名相同。
#若用戶在configure命令執行時,使用了參數--user=usergroup 和 --group=groupname,此時nginx.conf將使用參數中指定的用戶和用戶組。
#user nobody;
##Nginx worker進程個數:其數量直接影響性能。
#每個worker進程都是單線程的進程,他們會調用各個模塊以實現多種多樣的功能。如果這些模塊不會出現阻塞式的調用,那麼,有多少CPU內核就應該配置多少個進程,反之,有可能出現阻塞式調用,那麼,需要配置稍多一些的worker進程。
worker_processes 1;
##綁定Nginx worker進程到指定的CPU內核
#語法:worker_cpu_affinity cpumask[cpumask...]
#例如有4個CPU內核
#worker_cpu_affinity 1000 0100 0010 0001;
##ssl硬件加速。
#用戶可以用OpneSSL提供的命令來查看是否有ssl硬件加速設備:openssl engine -t
#ssl_engine device;
##系統調用gettimeofday的執行頻率
#語法: timer_resolution t
##Nginx worker進程優先級設置
#語法: worker_priority nice;
#默認: worker_priority 0;
##守護進程(daemon)。是脫離終端在後臺允許的進程。它脫離終端是爲了避免進程執行過程中的信息在任何終端上顯示。這樣一來,進程也不會被任何終端所產生的信息所打斷。##
##關閉守護進程的模式,之所以提供這種模式,是爲了方便跟蹤調試nginx,畢竟用gdb調試進程時最繁瑣的就是如何繼續跟進fork出的子進程了。##
##如果用off關閉了master_proccess方式,就不會fork出worker子進程來處理請求,而是用master進程自身來處理請求
#daemon off; #查看是否以守護進程的方式運行Nginx 默認是on
#master_process off; #是否以master/worker方式工作 默認是on
##error日誌的設置#
#語法: error_log /path/file level;
#默認: error_log / log/error.log error;
#當path/file 的值爲 /dev/null時,這樣就不會輸出任何日誌了,這也是關閉error日誌的唯一手段;
#leve的取值範圍是debug、info、notice、warn、error、crit、alert、emerg從左至右級別依次增大。
#當level的級別爲error時,error、crit、alert、emerg級別的日誌就都會輸出。大於等於該級別會輸出,小於該級別的不會輸出。
#如果設定的日誌級別是debug,則會輸出所有的日誌,這一數據量會很大,需要預先確保/path/file所在的磁盤有足夠的磁盤空間。級別設定到debug,必須在configure時加入 --with-debug配置項。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
##用來幫助用戶跟蹤調試Nginx 接收兩個參數stop和abort 通常不會使用
#stop Nginx的代碼執行到這些調試點就會發出SIGSTOP信號以用於調試
#abort 會產生一個coredump文件,可以使用gdb來查看Nginx當時的各種信息
#debug_points point[stop|abort]
##限制coredump核心轉儲文件的大小
#Nginx強制停止,將信息存儲在core文件中,以做調試使用。一個core文件可能達到幾GB,所以需要加以限制
#語法:worker_rlimit_core size
#worker_rlimit_core 1g;
##指定coredump文件生成目錄
#語法:working_directory path;
#working_directory ./core;
##指定Nginx worker進程可以打開的最大句柄描述符個數
#語法:worker_rlimit_nofile limit
##設置每個用戶發往Nginx的信號隊列的大小。
#也就是說用戶的信號隊列滿了,再發送就會被丟失
#語法:worker_rlimit_sigpending limit;
##pid文件(master進程ID的pid文件存放路徑)的路徑
#pid logs/nginx.pid;
##定義環境變量
#語法:env VAR|VAR=VALUE
#env TESTPATH=/tmp/;
##是否打開accept鎖 accept_mutex是Nginx的負載均衡鎖
#語法: accept_mutex[on|off]
#默認: accept_mutext on;
##lock文件的路徑
#語法: lock_file path/file;
#默認: lock_file logs/nginx.lock;
##使用accept鎖後到真正建立連接之間的延遲時間
#語法: accept_mutex_delay Nms;
#默認: accept_mutex_delay 500ms;
##批量建立新連接
#語法: multi_accept[on|off];
#默認: multi_accept off;
##選擇事件模型
#語法: use[kqueue|rtsig|epoll|/dev/poll|select|poll|eventport];
#默認: Nginx會自動使用最適合的事件模型。
#對於Linux操作系統來說, 可供選擇的事件驅動模型有poll、 select、 epoll三種。 epoll當
然是性能最高的一種,
events {
#僅對指定的客戶端輸出debug級別的日誌: 語法:debug_connection[IP|CIDR]
#這個設置項實際上屬於事件類配置,因此必須放在events{……}中才會生效。它的值可以是IP地址或者是CIRD地址。
#debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24
#這樣,僅僅以上IP地址的請求才會輸出debug級別的日誌,其他請求仍然沿用error_log中配置的日誌級別。
#注意:在使用debug_connection前,需確保在執行configure時已經加入了--with-debug參數,否則不會生效。
##每個worker的最大連接數
worker_connections 1024;
}
##核心轉儲(coredump):在Linux系統中,當進程發生錯誤或收到信號而終止時,系統會將進程執行時的內存內容(核心映像)寫入一個文件(core文件),以作爲調試只用,這就是所謂的核心轉儲(coredump).
http {
##嵌入其他配置文件 語法:include /path/file
#參數既可以是絕對路徑也可以是相對路徑(相對於Nginx的配置目錄,即nginx.conf所在的目錄)
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監聽的端口
#語法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ]
#default_server: 如果沒有設置這個參數,那麼將會以在nginx.conf中找到的第一個server塊作爲默認server塊
#backlog=num:表示TCP中backlog隊列的大小,默認-1,表示不予設置
#rcvbuf=size: 設置監聽句柄的SO_RCVBUF參數。
#sndbuf=size: 設置監聽句柄的SO_SNDBUF參數。
#accept_filter: 設置accept過濾器, 只對FreeBSD操作系統有用
#bind:綁定當前端口/地址,只有同時對一個端口監聽多個地址時
纔會生效。
#ssl: 在當前監聽的端口上建立的連接必須基於SSL協議。
listen 8080;
#主機名稱:其後可以跟多個主機名稱,開始處理一個HTTP請求時,nginx會取出header頭中的Host,與每個server中的server_name進行匹配,以此決定到底由那一個server來處理這個請求。有可能一個Host與多個server塊中的server_name都匹配,這時會根據匹配優先級來選擇實際處理的server塊。
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
##用來設置文件資源路徑的
#語法: alias path;
#配置塊: location
# alias usr/local/nginx/conf/;
# index index.html index.htm;
#}
##若請求的URI是/download/index/test.html, 那服務器就會返回服務器上optwebhtml、download/index/test.html文件的內容。
#location /download/ {
# root optwebhtml;
#}
##location 語法: location [=|~|~*|^~|@]/uri/ { ... }
#注意:location時有順序的,當一個請求有可能匹配多個location時,實際上這個請求會被第一個location處理。
location / {
proxy_pass http://192.168.1.60;
}
##按HTTP方法名限制用戶請求
#語法: limit_except method...{...}
#配置塊: location
#方法名可取值包括: GET、HEAD、 POST、 PUT、 DELETE、 MKCOL、 COPY、 MOVE、 OPTIONS、 PROPFIND、PROPPATCH、 LOCK、 UNLOCK或者PATCH。
#limit_except GET {
# allow 192.168.1.0/32;
# deny all;
#}
#注意, 允許GET方法就意味着也允許HEAD方法。 因此, 上面這段代碼表示的是禁止
GET方法和HEAD方法, 但其他HTTP方法是允許的。
##根據HTTP返回碼重定向頁面
#語法:error_page code[code...][=|=answer-code]uri|@named_location
#配置塊: http、 server、 location、 if
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
##是否允許遞歸使用error_page
#語法: recursive_error_pages[on|off];
#默認: recursive_error_pages off;
#配置塊: http、 server、 location
##嘗試按照順序訪問每一個path, 如果可以有效地讀取, 就直接向用戶返回這個path對應的文件結束請求, 否則繼續向下訪問。 如果所有的path都找不到有效的文件, 就重定向到最後的參數
uri上。
#因此最後一個path必須有url參數
#語法: try_files path1[path2]uri;
#配置塊: server、 location
location = /50x.html {
root html;
}
##爲了提高快速尋找到相應server name的能力, Nginx使用散列表來存儲server name。
#server_names_hash_bucket_size設置了每個散列桶佔用的內存大小。
#語法: server_names_hash_bucket_size size;
#默認: server_names_hash_bucket_size 32|64|128;
#配置塊: http、 server、 location
##server_names_hash_max_size會影響散列表的衝突率。 server_names_hash_max_size越大,
消耗的內存就越多, 但散列key的衝突率則會降低, 檢索速度也更快。
#語法: server_names_hash_max_size size;
#默認: server_names_hash_max_size 512;
#配置塊: http、 server、 location
##重定向主機名稱的處理
#該配置需要配合server_name使用。 在使用on打開時, 表示在重定向請求時會使用
server_name裏配置的第一個主機名代替原先請求中的Host頭部, 而使用off關閉時, 表示在重
定向請求時使用請求本身的Host頭部。
#語法: server_name_in_redirect on|off;
#默認: server_name_in_redirect on;
#配置塊: http、 server或者location
##HTTP包體只存儲到磁盤文件中
#當值爲非off時, 用戶請求中的HTTP包體一律存儲到磁盤文件中, 即使只有0字節也會存
儲爲文件。 當請求結束時, 如果配置爲on, 則這個文件不會被刪除(該配置一般用於調試、
定位問題) , 但如果配置爲clean, 則會刪除該文件。
#語法: client_body_in_file_only on|clean|off;
#默認: client_body_in_file_only off;
#配置塊: http、 server、 location
##HTTP包體儘量寫入到一個內存buffer中
#用戶請求中的HTTP包體一律存儲到內存buffer中。 當然, 如果HTTP包體的大小超過了
下面client_body_buffer_size設置的值, 包體還是會寫入到磁盤文件中。
#語法: client_body_in_single_buffer on|off;
#默認: client_body_in_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_buffers number size;
#默認: large_client_header_buffers 48k;
#配置塊: http、 server
##存儲HTTP包體的內存大小
#語法: client_body_buffer_siz size
#默認: client_body_buffer_siz 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
##指定內存池的初始大小
#語法: connection_pool_size size;
#默認: connection_pool_size 256;
#配置塊: http、 server
##指定每個請求的內存池大小
#語法: 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
##連接超時後將通過向客戶端發送RST包來直接重置連接
#語法: reset_timeout_connection on|off;
#默認: reset_timeout_connection off;
#配置塊: http、 server、 location
##該配置控制Nginx關閉用戶連接的方式。
#語法: lingering_close off|on|always;
#默認: lingering_close on;
#配置塊: http、 server、 location
##lingering_close啓用後, 這個配置項對於上傳大文件很有用
#語法: lingering_time time;
#默認: lingering_time 30s;配置塊: http、 server、 location
##lingering_close生效後, 在關閉連接前, 會檢測是否有用戶發送的數據到達服務器, 如果
超過lingering_timeout時間後還沒有數據可讀, 就直接關閉連接; 否則, 必須在讀取完連接緩
衝區上的數據並丟棄掉後纔會關閉連接。
#語法: lingering_timeout time;
#默認: lingering_timeout 5s;
#配置塊: http、 server、 location
##對某些瀏覽器禁用keepalive功能
#語法: keepalive_disable[msie6|safari|none]...
#默認: keepalive_disablemsie6 safari
#配置塊: http、 server、 location
##keepalive超時時間
#語法: keepalive_timeout time(默認單位: 秒) ;
#默認: keepalive_timeout 75;
#配置塊: http、 server、 location
##一個keepalive長連接上允許承載的請求最大數
#語法: keepalive_requests n;
#默認: keepalive_requests 100;
#配置塊: http、 server、 location
##確定對keepalive連接是否使用TCP_NODELAY選項
#語法: tcp_nodelay on|off;
#默認: tcp_nodelay on;
#配置塊: http、 server、 location
##在打開sendfile選項時, 確定是否開啓FreeBSD系統上的TCP_NOPUSH或Linux系統上的
TCP_CORK功能。 打開tcp_nopush後, 將會在發送響應時把整個響應包頭放到一個TCP包中
發送。
#語法: tcp_nopush on|off;
#默認: tcp_nopush off;
#配置塊: http、 server、 location
##HTTP請求包體的最大值
#語法: client_max_body_size size;
#默認: client_max_body_size 1m;
#配置塊: http、 server、 location
##對請求的限速
#此配置是對客戶端請求限制每秒傳輸的字節數。默認參數爲0, 表示不限速。
#語法: limit_rate speed;
#默認: limit_rate 0;
#配置塊: http、 server、 location、 if
##Nginx向客戶端發送的響應長度超過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
##此配置項在FreeBSD和Linux系統上使用O_DIRECT選項去讀取文件, 緩衝區大小爲size,
通常對大文件的讀取速度有優化作用。
#注意, 它與sendfile功能是互斥的。
#語法: directio size|off;
#默認: directio off;
#配置塊: http、 server、 location
##指定以directio方式讀取文件時的對齊方式
#它與directio配合使用
#語法: 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中的inactive參數配合使用
#語法: open_file_cache_min_uses number;
#默認: open_file_cache_min_uses 1;
#配置塊: http、 server、 location
##檢驗緩存中元素有效性的頻率
#默認爲每60秒檢查一次緩存中的元素是否仍有效
#語法: 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
--------------------負載均衡、反向代理----------------------------
##upstream塊,定義一個上游服務器集羣
#語法: upstream name{...}
#配置塊: http
upstream backserver{
##上游服務名可以爲域名、ip端口
#參數說明:
- weight=1 權重;
- max_fails=1與fail_time=10s配合使用,表示在10s內,上游服務器失敗次數超過1次,則不可用
- down:表示所在的上游服務器永久下線,只能使用ip_hash配置項時才能用
- backup:在使用ip_hash配置項時它是無效的,指備份服務器,表示所有非備份服務器都失效,纔會執行
server 192.168.1.2:90;
##ip_hash 有效地管理集羣中相同的緩存信息
#ip_hash與weight配置不可同時使用
ip_hash;
}
##反向代理的基本配置
#proxy_pass
#語法: proxy_pass URL;
#配置塊: location、 if
location / {
proxy_pass http://backserver; #配置負載均衡
#proxy_pass http://localhost:8080/test
#注:默認情況,反向代理不會轉發請求中的Host頭部,如需,添加
proxy_set_header Host $host;
#proxy_method表示轉發時的協議方法名。
#語法: proxy_method method;
#配置塊: http、 server、 location
proxy_method POST; #發來get請求,也會轉化成POST
#proxy_hide_header 指定HTTP頭部字段哪些可以轉發,默認不會轉發
#語法: proxy_hide_header the_header;
#配置塊: http、 server、 location
proxy_hide_header Cache-Control;
proxy_hide_header MicrosoftOfficeWebServer;
#proxy_pass_header:將原來禁止轉發的header設置爲允許
轉發
#語法: proxy_pass_header the_header;
#配置塊: http、 server、 location
proxy_pass_header X-Accel-Redirect;
#proxy_pass_request_body:確定是否向上遊服務器發送HTTP包體部分
#語法: proxy_pass_request_body on|off;
#默認: proxy_pass_request_body on;
#配置塊: http、 server、 location
proxy_pass_request_body off;
#proxy_pass_request_headers:確定是否轉發HTTP頭部
#語法: proxy_pass_request_headers on|off;
#默認: proxy_pass_request_headers on;
#配置塊: http、 server、 location
#proxy_redirect:當上遊服務器返回的響應是重定向或刷新請求(301或者302) 時,proxy_redirect可以重設HTTP頭部的location或refresh字段。
#語法: proxy_redirect[default|off|redirect replacement];
#參數解釋:off:使location或者refresh字段維持不變。
#默認: proxy_redirect default;
#配置塊: http、 server、 location
proxy_redirect off;
#proxy_next_upstream:當一臺上游服務轉發請求出現錯誤時,繼續換一臺上游服務器轉發請求
#語法:proxy_next_upstream[error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off];
#默認: proxy_next_upstream error timeout;
#配置塊: http、 server、 location
proxy_next_upstream timeout;
}
}
}
server_name與Host的匹配優先級
server_name與Host的匹配優先級 | 案例 |
---|---|
首先選擇所有字符串完全匹配的server_name | 如:www.testwab.com |
其次選擇通配符在前面的server_name | 如:*.testwab.com |
其次選擇通配符在後面的server_name | 如:www.testwab.* |
最後選擇使用正在表達式才匹配的server_name | 最後選擇使用正在表達式才匹配的server_name |
location的匹配規則
符號 | 解釋 |
---|---|
= | 表示把URI作爲字符串, 以便與參數中的uri做完全匹配。 |
~ | 表示匹配URI時是字母大小寫敏感的 |
~* | 表示匹配URI時忽略字母大小寫問題 |
^~ | 表示匹配URI時只需要其前半部分與uri參數匹配即可 |
@ | 僅用於Nginx服務內部請求之間的重定向, 帶有@的location不直接處理用戶請求 |
root和alias區別
如果有一個請求的URI是/conf/nginx.conf, 而用戶實際想訪問的文件
在usr/local/nginx/conf/nginx.conf。alias:
alias在實際文件路徑的映射過程中,已經將location後配置的/conf這部分去掉了
location conf { alias usr/local/nginx/conf/; }
root:
而root沒有去掉,這也是root可以放置到其他http、server等中,而alias只能放置location中的原因
location conf{ root usr/local/nginx/; }
alias還可以添加正則表達式:
location ~ ^/test/(\w+)\.(\w+)$ { alias usrlocal/nginx/$2/$1.$2; }
請求訪問/test/nginx.conf時,返回usr/local/nginx/conf/nginx.conf