NGINX介绍及参数
生活随笔
收集整理的這篇文章主要介紹了
NGINX介绍及参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Nginx("engine x")是一個IMAP/POP3/SMTP代理服務器,也是一個高性能的 HTTP 和 反向代理服務器,但現在大多數情況下都是用來做靜態web服務器和反向代理服務器,在作為反向代理服務器的時候,Nginx可以對后端的real server做負載均衡,基于應用層的負載均衡,但是他僅支持一些常見的協議,如:http ,mysql, ftp, smtp.
???????? 相較apache來說,Nginx在靜態web,反向代理,性能,高并發等功能上比apache要強大,但是apache在穩定性,動態網站的方面比Nginx優秀.隨著互聯網的規模越來越大,apache已經解決不了C10k(并發訪問量超過10k)的問題了,所以出現了新的應用程序,就如Nginx,目前Nginx的穩定版是1.4.*系列.
Nginx的特性:
1: #user nobody; #以哪個用戶的身份運行worker進程, 2: worker_processes 1; #啟動的worker的數量,性能優化關鍵點 3: 4: #error_log logs/error.log; #錯誤日志文件及其級別;默認為error級別;調試時可以使用debug級別,但要求在編譯時必須使用--with-debug啟用debug功能; 5: #error_log logs/error.log notice; #同上,但是級別為notice 6: #error_log logs/error.log info; #同上,但是級別為 info 7: 8: #pid logs/nginx.pid; #指定Nginx的pid文件 9: 10: 11: events { # 12: worker_connections 1024; #worker進程的個數;通常其數值應該為CPU的物理核心數減1或減2;Nginx的最大并發數為worker_processes*worker_connections 13: } 14: ----------------------以下都是默認不存在的配置------------------------- 15: -------性能優化相關------- 16: 17: worker_rlimit_nofile 9999; #指定一個worker進程所能夠打開的最大文件句柄數,可以使用ulimit -n查看單個文件可以打開的最大句柄數,(socket連接也算在里面)。系統默認值1024,此值需大于等于worker_connections,但是如果是代理服務器,此值應大于等于worker_connections的兩倍 18: worker_rlimit_sigpending 12; #設定每個用戶能夠發往worker進程的信號的數量 19: worker_cpu_affinity cpumask; #與上面的worker_processes有關,讓worker運行在指定的cpu上,如有4顆cpu,4個process,那就分別是0001,0010,0100,1000,表示第0,1,2,3顆cpu,性能優化關鍵點 20: ssl_engine ssldevice; #在存在ssl硬件加速器的服務器上,指定所使用的ssl硬件加速設備; 21: timer_resolution t; #每次內核事件調用返回時,都會使用gettimeofday()來更新nginx緩存時鐘;timer_resolution用于定義每隔多久才會由gettimeofday()更新一次緩存時鐘;x86-64系統上,gettimeofday()代價已經很小,可以忽略此配置; 22: worker_priority number; #worker的優先級,值越小,優先級越高,對于性能的增強也是很關鍵的(-20,20),默認是0,對系統的性能增強很關鍵 23: -------事件相關----------- 24: 需要定義在 events { } 里面 25: accept_mutex [on|off]; #是否打開Ningx的負載均衡鎖;此鎖能夠讓多個worker進輪流地、序列化地與新的客戶端建立連接;而通常當一個worker進程的負載達到其上限的7/8,master就盡可能不再將請求調度此worker;默認為on 26: lock_file /path/to/lock_file; #這是accept_mutex的鎖文件,如果accept_mutex為off,這項就沒用了 27: accept_mutex_delay #ms; #在accept鎖模式中,一個worker進程為取得accept鎖的等待時長;如果某worker進程在某次試圖取得鎖時失敗了,至少要等待#ms才能再一次請求鎖;默認是500毫秒 28: multi_accept on|off; #是否允許worker一次性地響應多個用戶請求;默認為Off,一個連接一個連接的建立; 29: use [epoll|rtsig|select|poll]; #定義使用的事件模型,建議讓nginx自動選擇;在Linux中一般(默認)都選擇epoll 30: ------用于調試、定位問題-- 31: 只在調試時使用 32: daemon on|off; #是否讓ningx運行后臺;默認為on,調試時可以設置為off,使得所有信息去接輸出控制臺; 33: master_process on|off; #是否以master/worker模式運行nginx;默認為on;調試時可設置off以方便追蹤;http段
1: ----主要有三段:http段,server段,location段------- 2: http { 3: include mime.types; 4: default_type application/octet-stream; 5: 6: #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 7: # '$status $body_bytes_sent "$http_referer" ' 8: # '"$http_user_agent" "$http_x_forwarded_for"'; 9: 10: #access_log logs/access.log main; 11: 12: sendfile on; 13: #tcp_nopush on; 14: 15: #keepalive_timeout 0; 16: keepalive_timeout 65; 17: 18: #gzip on; 19: 20: server { #定義虛擬主機 21: listen 80; #監聽的端口listen address[:port];listen port 22: server_name localhost; #定義基于主機名或IP的虛擬主機,可跟多個主機名 23: server_name_hash_bucket_size #快速主機名查找使用hash主機名 24: 25: #charset koi8-r; 26: 27: #access_log logs/host.access.log main; 28: 29: location / { 30: root html; #設置web資源的路徑,可以放在http,server和location中 31: index index.html index.htm; #定義默認主頁,自左向右匹配 32: } 33: 34: #error_page 404 [=200] /404.html; #錯誤頁面重定向,[=200表示請求狀態碼也編程200] 35: # redirect server error pages to the static page /50x.html 36: # 37: error_page 500 502 503 504 /50x.html; 38: location = /50x.html { 39: root html; 40: } 41: } 42: -----詳細說明------- 43: listen #僅能在server段 44: listen address[:port]; 45: listen port 46: default_server:定義此server為http中默認的server;如果所有的server中沒有任何一個listen使用此參數,那么第一個server即為默認server; 47: rcvbuf=SIZE: 接收緩沖大小; 48: sndbuf=SIZE: 發送緩沖大小; 49: ssl: https server; 50: server_name [...]; 51: server_name可以跟多個主機名,名稱中可以使用通配符和正則表達式(通常以~開頭);當nginx收到一個請求時,會取出其首部的server的值,而后跟眾server_name進行比較;比較方式: 52: (1) 先做精確匹配;www.magedu.com 53: (2) 左側通配符匹配;*.magedu.com 54: (3) 右側通配符匹配;www.abc.com, www.* 55: (4) 正則表達式匹配: ~^.*\.magedu\.com$ 56: server_name_hash_bucket_size 32|64|128; 57: 為了實現快速主機查找,nginx使用hash表來保存主機名;系統默認沒有此項,也不是太重要 58: ---------以下均為location中的參數------ 59: location [ = | ~ | ~* | ^~ ] uri { ... } 只能在server和location中,一個server中可以有多個location 60: location @name { ... } 61: 功能:允許根據用戶請求的URI來匹配指定的各location以進行訪問配置;匹配到時,將被location塊中的配置所處理;比如:http://www.magedu.com/images/logo.gif /images/logo.gif/就是URI 62: =:精確匹配;優先級最高 63: ~:正則表達式模式匹配,匹配時區分字符大小寫 64: ~*:正則表達式模式匹配,匹配時忽略字符大小寫 65: ^~: URI前半部分匹配,不檢查正則表達式 66: 讓我們用一個例子解釋上面的說法: 67: location = / { 68: [ configuration A ] 69: } 70: 71: location / { 72: [ configuration B ] 73: } 74: 75: location /documents/ { 76: [ configuration C ] 77: } 78: 79: location ^~ /images/ { 80: [ configuration D ] 81: } 82: 83: location ~* \.(gif|jpg|jpeg)$ { 84: [ configuration E ] 85: } 86: 請求“/”匹配配置A, 請求“/index.html”匹配配置B, 請求“/documents/document.html”匹配配置C, 請求“/images/1.gif”匹配配置D, 請求“/documents/1.jpg”匹配配置E。 87: 88: 1.alias path 89: 只能用于location中,用于路徑別名; 90: location /i/ { 91: alias /data/w3/images/; 92: } 93: “訪問/i/top.gif時”將由/data/w3/images/top.gif文件來響應。 94: 2.try_files path1 [path2 ...] uri;自左至右嘗試讀取由path所指定路徑,在第一次找到即停止并返回;如果所有path均不存在,則返回最后一個uri; 95: location ~* ^/documents/(.*)$ { 96: root /www/htdocs; 97: try_files $uri /docu/$1 /temp.html; 98: }網絡連接相關的設置
1: keepalive_timeout time; #保持連接的超時時長;默認為75秒;可以定義在http, server, location中 2: keepalive_requests n; #在一次長連接上允許承載的最大請求數;上下文:http,server,location 3: keepalive_disable [msie6 | safari | none ]; #對指定的瀏覽器禁止使用長連接;有些瀏覽器不支持長連接 4: tcp_nodelay on|off; #對keepalive連接是否使用TCP_NODELAY選項;默認on;把多個確認報文合成一個響應,確認延遲 5: client_header_timeout time; #讀取http請求首部的超時時長; 6: client_body_timeout time; #讀取http請求包體的超時時長; 7: send_timeout time; #發送響應的超時時長;對客戶端請求的限制:
1: limit_except method ... { ... } #指定對范圍之外的其它方法的訪問控制;指定method為GET方法的同時,nginx會自動添加HEAD方法。上下文:location 2: 如: limit_except GET { 3: allow 192.168.1.0/32; 4: deny all; 5: } 6: 請留意上面的例子將對除GET和HEAD方法以外的所有HTTP方法的請求進行訪問限制。 7: client_max_body_size SIZE; #http請求包體的最大值;常用于限定客戶所能夠請求的最大包體;根據請求首部中的Content-Length來檢測,以避免無用的傳輸;上下文: http, server, location 8: limit_rate speed; #限制客戶端每秒鐘傳輸的字節數;默認為0,表示沒有限制;上下文:http, server, location, if in location 9: limit_rate_after time; #nginx向客戶發送響應報文時,如果時長超出了此處指定的時長,則后續的發送過程開始限速;如:下載站上下文: http, server, location, if in location文件操作的優化
1: sendfile on|off是否啟用sendfile功能;由內核直接響應用戶請求,默認off上下文:http, server, location, if in location 2: aio on|off是否啟用aio功能;完全異步 3: open_file_cache max=N [inactive=time]|off是否打開文件緩存功能;上下文: http, server, location 4: max: 緩存條目的最大值;當滿了以后將根據LRU(最近最少使用)算法進行置換; 5: inactive: 某緩存條目在指定時長時沒有被訪問過時,將自動被刪除;默認為60s; 6: 緩存的信息包括: 7: 文件句柄、文件大小和上次修改時間; 8: 已經打開的目錄結構; 9: 沒有找到或沒有訪問權限的信息; 10: open_file_cache_errors on|off是否緩存文件找不到或沒有權限訪問等相關信息;默認off;上下文:http, server, location 11: open_file_cache_valid time;多長時間檢查一次緩存中的條目是否超出非活動時長,默認為60s; 上下文:http, server, location 12: open_file_cache_min_use #;在inactive指定的時長內被訪問超此處指定的次數地,才不會被刪除;默認1分鐘;上下文:http, server, location對客戶端請求的特殊處理
1: ignore_invalid_headers on|off是否忽略不合法的http首部;默認為on; off意味著請求首部中出現不合規的首部將拒絕響應;只能用于server和http; 2: log_not_found on|off; 是否將文件找不到的信息也記錄進錯誤日志中;默認為on;上下文:http, server, location 3: resolver address; 指定nginx使用的dns服務器地址;上下文: http, server, location 4: resover_timeout time;指定DNS解析超時時長,默認為30s,建議5秒??; 上下文:http, server, location 5: server_tokens on|off;是否在錯誤頁面中顯示nginx的版本號;默認on,上下文: http, server, locationhttp核心模塊的內置變量:
1: $uri: #當前請求的uri,不帶參數;參數 ?=12等之類的 2: $request_uri: #請求的uri,帶完整參數; 3: $host: #http請求報文中host首部;如果請求中沒有host首部,則以處理此請求的虛擬主機的主機名代替; 4: $hostname: #nginx服務運行在的主機的主機名; 5: $remote_addr: #客戶端IP 6: $remote_port: #客戶端Port 7: $remote_user: #使用用戶認證時客戶端用戶輸入的用戶名; 8: $request_filename: #用戶請求中的URI經過本地root或alias轉換后映射的本地的文件路徑;用的很多 9: $request_method: #請求方法 10: $server_addr: #服務器地址 11: $server_name: #服務器名稱 12: $server_port: #服務器端口 13: $server_protocol: #服務器向客戶端發送響應時的協議,如http/1.1, http/1.0 14: $scheme: #在請求中使用scheme, 如https://www.magedu.com/中的https; 15: $http_HEADER: #匹配請求報文中指定的HEADER,$http_host匹配請求報文中的host首部 16: $sent_http_HEADER: #匹配響應報文中指定的HEADER,例如$http_content_type匹配響應報文中的content-type首部; 17: $document_root: #當前請求映射到的root配置;配置使用nginx:
nginx虛擬主機
1: server { 2: listen 80; 3: server_name www.a.com; 4: root /web/a 5: }訪問控制access模塊,它只有兩個選項 allow和deny
1: server { 2: listen 80; 3: server_name www.b.org; 4: root /web/b; 5: 6: allow 192.168.0.0/16; 7: deny all; 8: }用戶認證示例
1: location /admin/ { 2: auth_basic "admin"; 3: auth_basic_user_file /etc/nginx/.htpasswd; 4: root /web/b/; 5: } 6: 使用htpasswd -c -m /path/to/somefile username 創建密碼文件建立下載站點autoindex,只需autoindex為on就行了,在/web/b/down目錄下的不可識別的文件都會被以列表的形式列出來
1: location /down/ { 2: autoindex on; 3: root /web/b/; 4: } 5:防盜鏈
1: 該指令的參數可以為下面的內容: 2: 3: none # 缺少“Referer”請求頭; 4: blocked # “Referer” 請求頭存在,但是它的值被防火墻或者代理服務器刪除; 這些值都不以“http://” 或者 “https://”字符串作為開頭; 5: server_names # “Referer” 請求頭包含某個虛擬主機名; 6: 任意字符串 # 定義一個服務器名和可選的URI前綴。服務器名允許在開頭或結尾使用“*”符號。 當nginx檢查時,“Referer”請求頭里的服務器端口將被忽略。 7: 正則表達式 # 必須以“~”符號作為開頭。 需要注意的是表達式會從“http://”或者“https://”之后的文本開始匹配。 8: 9: ngx_http_referer_module模塊允許攔截“Referer”請求頭中含有非法值的請求,阻止它們訪問站點。 需要注意的是偽造一個有效的“Referer”請求頭是相當容易的, 因此這個模塊的預期目的不在于徹底地阻止這些非法請求,而是為了阻止由正常瀏覽器發出的大規模此類請求。 還有一點需要注意,即使正常瀏覽器發送的合法請求,也可能沒有“Referer”請求頭。 10: 實例: 11: 12: valid_referers none blocked server_names 13: *.example.com example.* www.example.org/galleries/ 14: ~\.google\.; 15: 16: if ($invalid_referer) { 如果是以上指定范圍外的,則返回 403錯誤 17: return 403; 18: }URL rewrite,地址重寫
1: rewrite regex replacement [flag]; #上下文 server,location,if 2: 3: flag參數可以是其中之一: 4: 5: last 6: 停止執行當前這一輪的ngx_http_rewrite_module指令集,然后查找匹配改變后URI的新location; 7: break 8: 停止執行當前這一輪的ngx_http_rewrite_module指令集; 9: redirect 10: 在replacement字符串未以“http://”或“https://”開頭時,使用返回狀態碼為302的臨時重定向; 11: permanent 12: 返回狀態碼為301的永久重定向。 13: 舉例: 14: 15: server { 16: ... 17: rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; 18: rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; 19: return 403; 20: ... 21: } 22: 但是當上述指令寫在“/download/”的location中時,應使用標志break代替last,否則nginx會重復10輪循環,然后返回錯誤500: 23: 24: location /download/ { 25: rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; 26: rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; 27: return 403; 28: } 29: location /down1/ { #可以沒有down1文件夾 30: # root /web/b; 31: rewrite ^/down1/(.*\.(jpg|gif|png))$ /image/$1 last; #訪問網址是www.a.com/down1,實際目錄是www.a.com/image 32: } 33: 34: rewrite_log on|off #開啟或者關閉將ngx_http_rewrite_module模塊指令的處理日志以notice級別記錄到錯誤日志中。默認為off; 35: return code: #用于結束rewrite規則,并且為客戶返回狀態碼;可以使用的狀態碼有204, 400, 402-406, 500-504等;SSL
1: # 2: #server { 3: # listen 443; 4: # server_name localhost; 5: 6: # ssl on; #開啟ssl引擎 7: # ssl_certificate cert.pem; #證書 8: # ssl_certificate_key cert.key; #私鑰文件 9: 10: # ssl_session_timeout 5m; #ssl的超時時間,ssl會話的建立還釋放比保持連接更消耗時間,所以如果內存夠大并且你網站的粘性比較大,建議時間調長一點 11: 12: # ssl_protocols SSLv2 SSLv3 TLSv1; #支持的協議 13: # ssl_ciphers HIGH:!aNULL:!MD5; #加密算法 14: # ssl_prefer_server_ciphers on; #服務端選擇傾向的算法stub_status狀態頁
1: location /server-status { 2: stub_status on; 3: } 4: Active connections: 2 5: server accepts handled requests 6: 2 2 1 7: Reading: 0 Writing: 1 Waiting: 1 8: 9: active connections -- number of all open connections 10: server accepts handled requests -- nginx accepted 2 connections, handled 2 connections (no one was closed just it was accepted), and handles 1 requests (1.8 requests per connection) 11: reading -- nginx reads request header 12: writing -- nginx reads request body, processes request, or writes response to a client 13: waiting -- keep-alive connections, actually it is active - (reading + writing)gzip壓縮頁面
1: http { 2: gzip on; #開啟壓縮功能,可以放在http,server 3: gzip_http_version 1.0; #壓縮后使用那種http協議構建響應報文 4: gzip_comp_level 2; #壓縮級別 5: gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json; #僅對這些格式的內容進行壓縮 6: gzip_disable msie6; #對IE6不使用壓縮機制 7: }總結
以上是生活随笔為你收集整理的NGINX介绍及参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么查看linux服务器有cpu
- 下一篇: nginx 简介篇