時間:2023-06-29 18:24:01 | 來源:網(wǎng)站運營
時間:2023-06-29 18:24:01 來源:網(wǎng)站運營
為了徹底理解 Nginx,一怒之下我把它的核心知識點扒了個底朝天:server { # 第一個Server區(qū)塊開始,表示一個獨立的虛擬主機站點 listen 80; # 提供服務(wù)的端口,默認80 server_name localhost; # 提供服務(wù)的域名主機名 location / { # 第一個location區(qū)塊開始 root html; # 站點的根目錄,相當于Nginx的安裝目錄 index index.html index.html; # 默認的首頁文件,多個用空格分開 } # 第一個location區(qū)塊結(jié)果
[root@localhost ~]# tree /usr/local/nginx/usr/local/nginx├── client_body_temp├── conf # Nginx所有配置文件的目錄│ ├── fastcgi.conf # fastcgi相關(guān)參數(shù)的配置文件│ ├── fastcgi.conf.default # fastcgi.conf的原始備份文件│ ├── fastcgi_params # fastcgi的參數(shù)文件│ ├── fastcgi_params.default │ ├── koi-utf│ ├── koi-win│ ├── mime.types # 媒體類型│ ├── mime.types.default│ ├── nginx.conf # Nginx主配置文件│ ├── nginx.conf.default│ ├── scgi_params # scgi相關(guān)參數(shù)文件│ ├── scgi_params.default │ ├── uwsgi_params # uwsgi相關(guān)參數(shù)文件│ ├── uwsgi_params.default│ └── win-utf├── fastcgi_temp # fastcgi臨時數(shù)據(jù)目錄├── html # Nginx默認站點目錄│ ├── 50x.html # 錯誤頁面優(yōu)雅替代顯示文件,例如當出現(xiàn)502錯誤時會調(diào)用此頁面│ └── index.html # 默認的首頁文件├── logs # Nginx日志目錄│ ├── access.log # 訪問日志文件│ ├── error.log # 錯誤日志文件│ └── nginx.pid # pid文件,Nginx進程啟動后,會把所有進程的ID號寫到此文件├── proxy_temp # 臨時目錄├── sbin # Nginx命令目錄│ └── nginx # Nginx的啟動命令├── scgi_temp # 臨時目錄└── uwsgi_temp # 臨時目錄
worker_processes 1; # worker進程的數(shù)量events { # 事件區(qū)塊開始 worker_connections 1024; # 每個worker進程支持的最杭州接數(shù)} # 事件區(qū)塊結(jié)束http { # HTTP區(qū)塊開始 include mime.types; # Nginx支持的媒體類型庫文件 default_type application/octet-stream; # 默認的媒體類型 sendfile on; # 開啟高效傳輸模式 keepalive_timeout 65; # 連接超時 server { # 第一個Server區(qū)塊開始,表示一個獨立的虛擬主機站點 listen 80; # 提供服務(wù)的端口,默認80 server_name localhost; # 提供服務(wù)的域名主機名 location / { # 第一個location區(qū)塊開始 root html; # 站點的根目錄,相當于Nginx的安裝目錄 index index.html index.htm; # 默認的首頁文件,多個用空格分開 } # 第一個location區(qū)塊結(jié)果 error_page 500502503504 /50x.html; # 出現(xiàn)對應(yīng)的http狀態(tài)碼時,使用50x.html回應(yīng)客戶 location = /50x.html { # location區(qū)塊開始,訪問50x.html root html; # 指定對應(yīng)的站點目錄為html } } ......
location /image/ { root /usr/local/static/; autoindex on;}
步驟:# 創(chuàng)建目錄mkdir /usr/local/static/image # 進入目錄cd /usr/local/static/image # 上傳照片photo.jpg # 重啟nginxsudo nginx -s reload
打開瀏覽器 輸入 server_name/image/1.jpg 就可以訪問該靜態(tài)圖片了upstream backserver { server 192.168.0.12; server 192.168.0.13; }
# 權(quán)重越高,在被訪問的概率越大,如上例,分別是20%,80%。upstream backserver { server 192.168.0.12 weight=2; server 192.168.0.13 weight=8; }
upstream backserver { ip_hash; server 192.168.0.12:88; server 192.168.0.13:80; }
# 哪個服務(wù)器的響應(yīng)速度快,就將請求分配到那個服務(wù)器上。upstream backserver { server server1; server server2; fair; }
upstream backserver { server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32; }
# 當客戶端訪問www.lijie.com,監(jiān)聽端口號為80,直接跳轉(zhuǎn)到data/www目錄下文件server { listen 80; server_name www.lijie.com; location / { root data/www; index index.html index.htm; }} # 當客戶端訪問www.lijie.com,監(jiān)聽端口號為80,直接跳轉(zhuǎn)到data/bbs目錄下文件 server { listen 80; server_name bbs.lijie.com; location / { root data/bbs; index index.html index.htm; }}
# 當客戶端訪問www.lijie.com,監(jiān)聽端口號為8080,直接跳轉(zhuǎn)到data/www目錄下文件 server { listen 8080; server_name 8080.lijie.com; location / { root data/www; index index.html index.htm; }}# 當客戶端訪問www.lijie.com,監(jiān)聽端口號為80直接跳轉(zhuǎn)到真實ip服務(wù)器地址 127.0.0.1:8080server { listen 80; server_name www.lijie.com; location / { proxy_pass http://127.0.0.1:8080; index index.html index.htm; }}
# 優(yōu)先級1,精確匹配,根路徑location =/ { return 400;} # 優(yōu)先級2,以某個字符串開頭,以av開頭的,優(yōu)先匹配這里,區(qū)分大小寫location ^~ /av { root /data/av/;} # 優(yōu)先級3,區(qū)分大小寫的正則匹配,匹配/media*****路徑location ~ /media { alias /data/static/;} # 優(yōu)先級4 ,不區(qū)分大小寫的正則匹配,所有的****.jpg|gif|png 都走這里location ~* .*/.(jpg|gif|png|js|css)$ { root /data/av/;} # 優(yōu)先7,通用匹配location / { return 403;}
# 定義限流維度,一個用戶一分鐘一個請求進來,多余的全部漏掉limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m; # 綁定限流維度server{ location/seckill.html{ limit_req zone=zone; proxy_pass http://lj_seckill; } }
1r/s代表1秒一個請求,1r/m一分鐘接收一個請求, 如果Nginx這時還有別人的請求沒有處理完,Nginx就會拒絕處理該用戶請求。# 定義限流維度,一個用戶一分鐘一個請求進來,多余的全部漏掉limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m; # 綁定限流維度server{ location/seckill.html{ limit_req zone=zone burst=5 nodelay; proxy_pass http://lj_seckill; } }
為什么就多了一個 burst=5 nodelay; 呢,多了這個可以代表Nginx對于一個用戶的請求會立即處理前五個,多余的就慢慢來落,沒有其他用戶的請求我就處理你的,有其他的請求的話我Nginx就漏掉不接受你的請求http { limit_conn_zone $binary_remote_addr zone=myip:10m; limit_conn_zone $server_name zone=myServerName:10m;} server { location / { limit_conn myip 10; limit_conn myServerName 100; rewrite / http://www.lijie.net permanent; }}
上面配置了單個IP同時并發(fā)連接數(shù)最多只能10個連接,并且設(shè)置了整個虛擬服務(wù)器同時最大并發(fā)數(shù)最多只能100個鏈接。當然,只有當請求的header被服務(wù)器處理后,虛擬服務(wù)器的連接數(shù)才會計數(shù)。剛才有提到過Nginx是基于漏桶算法原理實現(xiàn)的,實際上限流一般都是基于漏桶算法和令牌桶算法實現(xiàn)的。server { listen 80; server_name www.lijie.com; location / { ### 指定上游服務(wù)器負載均衡服務(wù)器 proxy_pass http://backServer; ###nginx與上游服務(wù)器(真實訪問的服務(wù)器)超時時間 后端服務(wù)器連接的超時時間_發(fā)起握手等候響應(yīng)超時時間 proxy_connect_timeout 1s; ###nginx發(fā)送給上游服務(wù)器(真實訪問的服務(wù)器)超時時間 proxy_send_timeout 1s; ### nginx接受上游服務(wù)器(真實訪問的服務(wù)器)超時時間 proxy_read_timeout 1s; index index.html index.htm; } }
# 如果訪問的ip地址為192.168.9.115,則返回403if ($remote_addr = 192.168.9.115) { return 403; }
## 不允許谷歌瀏覽器訪問 如果是谷歌瀏覽器返回500 if ($http_user_agent ~ Chrome) { return 500; }
$remote_addr //獲取客戶端ip$binary_remote_addr //客戶端ip(二進制)$remote_port //客戶端port,如:50472$remote_user //已經(jīng)經(jīng)過Auth Basic Module驗證的用戶名$host //請求主機頭字段,否則為服務(wù)器名稱,如:blog.sakmon.com$request //用戶請求信息,如:GET ?a=1&b=2 HTTP/1.1$request_filename //當前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.html$status //請求的響應(yīng)狀態(tài)碼,如:200$body_bytes_sent // 響應(yīng)時送出的body字節(jié)數(shù)數(shù)量。即使連接中斷,這個數(shù)據(jù)也是精確的,如:40$content_length // 等于請求行的“Content_Length”的值$content_type // 等于請求行的“Content_Type”的值$http_referer // 引用地址$http_user_agent // 客戶端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36$args //與$query_string相同 等于當中URL的參數(shù)(GET),如a=1&b=2$document_uri //與$uri相同 這個變量指當前的請求URI,不包括任何參數(shù)(見$args) 如:/2013/81.html$document_root //針對當前請求的根路徑設(shè)置值$hostname //如:centos53.localdomain$http_cookie //客戶端cookie信息$cookie_COOKIE //cookie COOKIE變量的值$is_args //如果有$args參數(shù),這個變量等于”?”,否則等于”",空值,如?$limit_rate //這個變量可以限制連接速率,0表示不限速$query_string // 與$args相同 等于當中URL的參數(shù)(GET),如a=1&b=2$request_body // 記錄POST過來的數(shù)據(jù)信息$request_body_file //客戶端請求主體信息的臨時文件名$request_method //客戶端請求的動作,通常為GET或POST,如:GET$request_uri //包含請求參數(shù)的原始URI,不包含主機名,如:/2013/81.html?a=1&b=2$scheme //HTTP方法(如http,https),如:http$uri //這個變量指當前的請求URI,不包括任何參數(shù)(見$args) 如:/2013/81.html$request_completion //如果請求結(jié)束,設(shè)置為OK. 當請求未結(jié)束或如果該請求不是請求鏈串的最后一個時,為空(Empty),如:OK$server_protocol //請求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1$server_addr //服務(wù)器IP地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值$server_name //服務(wù)器名稱,如:blog.sakmon.com$server_port //請求到達服務(wù)器的端口號,如:80
ngx_http_proxy_module
和 ngx_http_upstream_module
對后端節(jié)點做健康檢查。nginx_upstream_check_module
模塊對后端節(jié)點做健康檢查。http { # 開啟gzip gzip on; # 啟用gzip壓縮的最小文件;小于設(shè)置值的文件將不會被壓縮 gzip_min_length 1k; # gzip 壓縮級別 1-10 gzip_comp_level 2; # 進行壓縮的文件類型。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 是否在http header中添加Vary: Accept-Encoding,建議開啟 gzip_vary on;}
保存并重啟nginx,刷新頁面(為了避免緩存,請強制刷新)就能看到效果了。以谷歌瀏覽器為例,通過F12看請求的響應(yīng)頭部:$date_gmt
和$date_local
的變量。Proxy_set_header THE-TIME $date_gmt;
fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;
fastcgi_buffer_size 32k;fastcgi_buffers 8 32k;
proxy_buffer_size 16k;proxy_buffers 4 16k;
作者:夏目 鏈接:http://blog.csdn.net/wuzhiwei549/article/details/122758937如果本文對你有幫助的話,歡迎點贊
關(guān)鍵詞:核心,知識,理解
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。