與套接字相關的配置javascript
(1)server { ... } 配置一個虛擬主機; server { listen address[:PORT]|PORT; server_name SERVER_NAME; root /PATH/TO/DOCUMENT_ROOT; } (2)listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size] default_server:設定爲默認虛擬主機; ssl:限制僅可以經過ssl鏈接提供服務; backlog=number:後援隊列長度; rcvbuf=size:接收緩衝區大小; sndbuf=size:發送緩衝區大小 (3)server_name name ...; 指明虛擬主機的主機名稱;後可跟多個由空白字符分隔的字符串; 支持*通配任意長度的任意字符;server_name *.magedu.com www.magedu.* 支持~起始的字符作正則表達式模式匹配;server_name ~^www\d+\.magedu\.com$ 匹配機制: (a) 首先是字符串精確匹配; (b) 左側*通配符; (c) 右側*通配符; (d) 正則表達式; (4)tcp_nodelay on | off; 在keepalived模式下的鏈接是否啓用TCP_NODELAY選項;表示在長鏈接模式下一個個發包,不打包一塊兒發 tcp_nopush on|off; 在sendfile模式下,是否啓用TCP_CORK選項;表示等於用戶空間把http首部送過來一塊兒發 (5)sendfile on | off; 是否啓用sendfile功能;表示把響應報文首部和文件開頭放在一個報文發送,在一個報文發送文件
2.定義路徑相關的配置:php
(1)root path; 設置web資源路徑映射;用於指明用戶請求的url所對應的本地文件系統上的文檔所在目錄路徑;可用的位置:http, server, location, if in location; (2)location [ = | ~ | ~* | ^~ ] uri { ... } 在一個server中location配置段可存在多個,用於實現從uri到文件系統的路徑映射;ngnix會根據用戶請求的URI來檢查定義的全部location,並找出一個最佳匹配,然後應用其配置; =:對URI作精確匹配; location = / { ... } ~:對URI作正則表達式模式匹配,區分字符大小寫; ~*:對URI作正則表達式模式匹配,不區分字符大小寫; ^~:對URI的左半部分作匹配檢查,不區分字符大小寫; 不帶符號:匹配起始於此uri的全部的url; 匹配優先級:=, ^~, ~/~*,不帶符號; root /vhosts/www/htdocs/ http://www.www.com/index.html --> /vhosts/www/htdocs/index.html server { root /vhosts/www/htdocs/ location /admin/ { root /webapps/app1/data/ } } (3)、alias path; 定義路徑別名,文檔映射的另外一種機制;僅能用於location上下文; 注意:location中使用root指令和alias指令的意義不一樣; (a) root,給定的路徑對應於location中的/uri/左側的/; (b) alias,給定的路徑對應於location中的/uri/右側的/ (4)index file ...; 默認資源;http, server, location; (5)error_page code ... [=[response]] uri; Defines the URI that will be shown for the specified errors.
3.定義客戶端請求的相關配置:css
(1)keepalive_timeout timeout [header_timeout];(負載均衡短,後端服務器長) 設定保持鏈接的超時時長,0表示禁止長鏈接;默認爲75s; (2)keepalive_requests number; 在一次長鏈接上所容許請求的資源的最大數量,默認爲100; (3)、keepalive_disable none | browser ...; 對哪一種瀏覽器禁用長鏈接; (4)send_timeout time; 向客戶端發送響應報文的超時時長,此處,是指兩次寫操做之間的間隔時長; (5)client_body_buffer_size size;(post上傳大文件時) 用於接收客戶端請求報文的body部分的緩衝區大小;默認爲16k;超出此大小時,其將被暫存到磁盤上的由 client_body_temp_path指令所定義的位置; (6)client_body_temp_path path [level1 [level2 [level3]]]; 設定用於存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量;
4.對客戶端進行限制的相關配置:html
(1)limit_rate rate; 限制響應給客戶端的傳輸速率,單位是bytes/second,0表示無限制; (2)limit_except method ... { ... } 限制對指定的請求方法以外的其它方法的使用客戶端; limit_except GET { allow 192.168.1.0/24; deny all; }
5.文件操做優化的配置:java
(1)aio on | off | threads[=pool]; 是否啓用aio功能; (2)directio size | off; 在Linux主機啓用O_DIRECT標記,此處意味文件大於等於給定的大小時使用,例如directio 4m; (3)open_file_cache off;(緩存元數據) open_file_cache max=N [inactive=time]; nginx能夠緩存如下三種信息: (a) 文件的描述符、文件大小和最近一次的修改時間; (b) 打開的目錄結構; (c) 沒有找到的或者沒有權限訪問的文件的相關信息; max=N:可緩存的緩存項上限;達到上限後會使用LRU算法(最近最少使用)實現緩存管理; inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少於open_file_cache_min_uses指令所指定的次數的緩存項即爲非活動項; (4)open_file_cache_valid time; 緩存項有效性的檢查頻率;默認爲60s; (5)open_file_cache_min_uses number; 在open_file_cache指令的inactive參數指定的時長內,至少應該被命中多少次方可被歸類爲活動項; (6)open_file_cache_errors on | off; 是否緩存查找時發生錯誤的文件一類的信息;
6.ngx_http_access_module模塊:
實現基於ip的訪問控制功能node
(1)allow address | CIDR | unix: | all; (2)deny address | CIDR | unix: | all;
7.ngx_http_auth_basic_module模塊:linux
(1)auth_basic string | off; (2)auth_basic_user_file file; location /admin/ { alias /webapps/app1/data/; auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.ngxpasswd; } 注意:htpasswd命令由httpd-tools所提供;
8.ngx_http_stub_status_module模塊:
用於輸出nginx的基本狀態信息;nginx
Active connections: 291 server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106 Active connections: 活動狀態的鏈接數; accepts:已經接受的客戶端請求的總數; handled:已經處理完成的客戶端請求的總數; requests:客戶端發來的總的請求數; Reading:處於讀取客戶端請求報文首部的鏈接的鏈接數; Writing:處於向客戶端發送響應報文過程當中的鏈接數; Waiting:處於等待客戶端發出請求的空閒鏈接數;
stub_status; 配置示例: location /basic_status { stub_status; }
9.ngx_http_log_module模塊web
(1)log_format name string ...; string可使用nginx核心模塊及其它模塊內嵌的變量; (2)access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; 訪問日誌文件路徑,格式及相關的緩衝的配置; buffer=size flush=time (3)open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; 緩存各日誌文件相關的元數據信息; max:緩存的最大文件描述符數量; min_uses:在inactive指定的時長內訪問大於等於此值方可被看成活動項; inactive:非活動時長; valid:驗正緩存中各緩存項是否爲活動項的時間間隔;
10.ngx_http_gzip_module:正則表達式
(1)gzip on | off; Enables or disables gzipping of responses. (2)gzip_comp_level level; Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9. (3)gzip_disable regex ...; Disables gzipping of responses for requests with 「User-Agent」 header fields matching any of the specified regular expressions. (4)gzip_min_length length; 啓用壓縮功能的響應報文大小閾值; (5)gzip_buffers number size; 支持實現壓縮功能時爲其配置的緩衝區數量及每一個緩存區的大小; (6)gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; nginx做爲代理服務器接收到從被代理服務器發送的響應報文後,在何種條件下啓用壓縮功能的; off:對代理的請求不啓用 no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值爲此三者中任何一個,則啓用壓縮功能; (7)gzip_types mime-type ...; 壓縮過濾器,僅對此處設定的MIME類型的內容啓用壓縮功能; 示例: gzip on; gzip_comp_level 6; gzip_min_length 64; gzip_proxied any; gzip_types text/xml text/css application/javascript;
8.ngx_http_ssl_module模塊:
(1) ssl on | off; Enables the HTTPS protocol for the given virtual server. (2)ssl_certificate file; 當前虛擬主機使用PEM格式的證書文件; (3)ssl_certificate_key file; 當前虛擬主機上與其證書匹配的私鑰文件; (4)ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; 支持ssl協議版本,默認爲後三個; (5)、ssl_session_cache off | none | [builtin[:size]] [shared:name:size]; builtin[:size]:使用OpenSSL內建的緩存,此緩存爲每worker進程私有; [shared:name:size]:在各worker之間使用一個共享的緩存; (6)ssl_session_timeout time; 客戶端一側的鏈接能夠複用ssl session cache中緩存 的ssl參數的有效時長; 配置示例: server { listen 443 ssl; server_name www.magedu.com; root /vhosts/ssl/htdocs; ssl on; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ssl_session_cache shared:sslcache:20m; }
9.ngx_http_rewrite_module模塊:
(1)rewrite regex replacement [flag] 將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換爲replacement指定的新的URI; 注意:若是在同一級配置塊中存在多個rewrite規則,那麼會自下而下逐個檢查;被某條件規則替換完成後,會從新一輪的替換檢查,所以,隱含有循環機制;[flag]所表示的標誌位用於控制此循環機制; last:重寫完成後中止對當前URI在當前location中後續的其它重寫操做,然後對新的URI啓動新一輪重寫檢查;提早重啓新一輪循環; break:重寫完成後中止對當前URI在當前location中後續的其它重寫操做,然後直接跳轉至重寫規則配置塊以後的其它配置;結束循環; redirect:重寫完成後以臨時重定向方式直接返回重寫後生成的新URI給客戶端,由客戶端從新發起請求;不能以http://或https://開頭;(301) permanent:重寫完成後以永久重定向方式直接返回重寫後生成的新URI給客戶端,由客戶端從新發起請求;(302) (2)return return code [text]; return code URL; return URL; Stops processing and returns the specified code to a client. (3)rewrite_log on | off; 是否開啓重寫日誌; (4) if (condition) { ... } 引入一個新的配置上下文 ;條件知足時,執行配置塊中的配置指令;server, location: condition: 比較操做符: == != ~:模式匹配,區分字符大小寫; ~*:模式匹配,不區分字符大小寫; !~:模式不匹配,區分字符大小寫; !~*:模式不匹配,不區分字符大小寫; 文件及目錄存在性判斷: -e, !-e -f, !-f -d, !-d -x, !-x
10.ngx_http_referer_module模塊
(1)valid_referers none | blocked | server_names | string ...; 定義referer首部的合法可用值; none:請求報文首部沒有referer首部; blocked:請求報文的referer首部沒有值; server_names:參數,其能夠有值做爲主機名或主機名模式; arbitrary_string:直接字符串,但可以使用*做通配符; regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭,例如 ~.*\.magedu\.com; 配置示例: valid_referers none block server_names *.magedungx_http_proxy_module模塊:.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.; if($invalid_referer) { return http://www.magedu.com/invalid.jpg; } if($invalid_referer) { return 403; }
11.ngx_http_proxy_module模塊:
(1)proxy_pass URL; Context: location, if in location, limit_except 注意:proxy_pass後面的路徑不帶uri時,其會將location的uri傳遞給後端主機 server { ... server_name HOSTNAME; location /uri/ { proxy http://host[:port]; } ... } http://HOSTNAME/uri --> http://host/uri proxy_pass後面的路徑是一個uri時,其會將location的uri替換爲proxy_pass的uri; server { ... server_name HOSTNAME; location /uri/ { proxy http://host/new_uri/; } .. } http://HOSTNAME/uri/ --> http://host/new_uri/ 若是location定義其uri時使用了正則表達式的模式,或在if語句或limt_execept中使用proxy_pass指令,則 proxy_pass以後必須不能使用uri; 用戶請求時傳遞的uri將直接附加代理到的服務的以後; server { ... server_name HOSTNAME; location ~|~* /uri/ { proxy http://host; } ... } (2)proxy_set_header field value; 設定發日後端主機的請求報文的請求首部的值;Context: http, server, location proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; (3)proxy_cache_path 定義可用於proxy功能的緩存;Context: http proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; (4)proxy_cache zone | off; 指明要調用的緩存,或關閉緩存機制;Context: http, server, location (5)proxy_cache_key string; 緩存中用於「鍵」的內容; 默認值:proxy_cache_key $scheme$proxy_host$request_uri; (6)proxy_cache_valid [code ...] time; 定義對特定響應碼的響應內容的緩存時長; 定義在http{...}中; proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g; 定義在須要調用緩存功能的配置段,例如server{...}; proxy_cache pxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; proxy_cache_use_stale http_502; (7)proxy_cache_use_stale(後端服務器出現了問題,反向代理用緩存來響應) proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...; Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server. (8)proxy_cache_methods GET | HEAD | POST ...; If the client request method is listed in this directive then the response will be cached. 「GET」 and 「HEAD」 methods are always added to the list, though it is recommended to specify them explicitly. (9)proxy_hide_header field; By default, nginx does not pass the header fields 「Date」, 「Server」, 「X-Pad」, and 「X-Accel-...」 from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed. (10)proxy_connect_timeout time; Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds. 默認爲60s;最長爲75s; (11)proxy_read_timeout time; Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. (12)proxy_send_timeout time; Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.
12.ngx_http_headers_module模塊:
向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值;
(1)add_header name value [always] 添加自定義首部; add_header X-Via $server_addr; add_header X-Accel $server_name; (2)expires [modified] time; expires epoch | max | off; 用於定義Expire或Cache-Control首部的值;
13.ngx_http_fastcgi_module模塊:
(1)fastcgi_pass address; address爲fastcgi server的地址; location, if in location; http://www.ilinux.io/admin/index.php --> /admin/index.php (uri) /data/application/admin/index.php (2)fastcgi_index name; fastcgi默認的主頁資源; (3)fastcgi_param parameter value [if_not_empty]; Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination. 配置示例1: 前提:配置好fpm server和mariadb-server服務; location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } 配置示例2:經過/pm_status和/ping來獲取fpm server狀態信息; location ~* ^/(pm_status|ping)$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } (4)fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; 定義fastcgi的緩存;緩存位置爲磁盤上的文件系統,由path所指定路徑來定義; levels=levels:緩存目錄的層級數量,以及每一級的目錄數量;levels=ONE:TWO:THREE leves=1:2:2 keys_zone=name:size k/v映射的內存空間的名稱及大小 inactive=time 非活動時長 max_size=size 磁盤上用於緩存數據的緩存空間上限 (5)fastcgi_cache zone | off; 調用指定的緩存空間來緩存數據;http, server, location (6)fastcgi_cache_key string; 定義用做緩存項的key的字符串; (7)fastcgi_cache_methods GET | HEAD | POST ...; 爲哪些請求方法使用緩存; (8)fastcgi_cache_min_uses number; 緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認做活動項; (9)fastcgi_cache_valid [code ...] time; 不一樣的響應碼各自的緩存時長; 示例: http { ... fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s; ... server { ... location ~* \.php$ { ... fastcgi_cache fcgi; fastcgi_cache_key $request_uri; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m; ... } ... } ... } (10)fastcgi_keep_conn on | off;(發請求時) By default, a FastCGI server will close a connection right after sending the response. However, when this directive is set to the value on, nginx will instruct a FastCGI server to keep connections open.
14.ngx_http_upstream_module模塊
(1)upstream name { ... } 定義後端服務器組,會引入一個新的上下文;Context: http upstream httpdsrvs { server ... server... ... } (2)server address [parameters]; 在upstream上下文中server成員,以及相關的參數;Context: upstream address的表示格式: unix:/PATH/TO/SOME_SOCK_FILE IP[:PORT] HOSTNAME[:PORT] parameters: weight=number 權重,默認爲1; max_fails=number 失敗嘗試最大次數;超出此處指定的次數時,server將被標記爲不可用; fail_timeout=time 設置將服務器標記爲不可用狀態的超時時長; max_conns 當前的服務器的最大併發鏈接數; backup 將服務器標記爲「備用」,即全部服務器均不可用時此服務器才啓用; down 標記爲「不可用」; (3)least_conn; 最少鏈接調度算法,當server擁有不一樣的權重時其爲wlc; (4)ip_hash; 源地址hash調度方法; (5)hash key [consistent]; 基於指定的key的hash表來實現對請求的調度,此處的key能夠直接文本、變量或兩者的組合; 做用:將請求分類,同一類請求將發往同一個upstream server; If the consistent parameter is specified the ketama consistent hashing method will be used instead. 示例: hash $request_uri consistent;(提升命中率) hash $remote_addr; (6)keepalive connections; 爲每一個worker進程保留的空閒的長鏈接數量;每worker與後端服務保持的最大空閒長鏈接數量
15.ngx_stream_core_module模塊:
(1)proxy_pass address; (2)proxy_timeout timeout:默認爲10s,鏈接創建起來了,請求發出去的超時時間 (3)proxy_connect_timeout time:設置nginx與被代理的服務器嘗試創建鏈接的超時時長;默認爲60s; 示例:stream { ... } 定義stream相關的服務;Context:main stream { upstream sshsrvs { server 192.168.22.2:22; server 192.168.22.3:22; least_conn; } server { listen 10.1.0.6:22022; proxy_pass sshsrvs; } }