本文已同步到專業技術網站 www.sufaith.com, 該網站專一於先後端開發技術與經驗分享, 包含Web開發、Nodejs、Python、Linux、IT資訊等板塊.
javascript
Nginx是一個高性能、輕量級的Web和反向代理服務器, 其特色是佔有內存及資源少、抗併發能力強。php
Nginx安裝簡單、配置簡潔、啓動快速便捷、支持熱部署、支持 SSL、擁有高度模塊化的設計。css
Nginx的主要功能有:html
備註: 如下命令中的 /usr/local/nginx 是nginx二進制文件的絕對路徑,需根據本身實際的安裝路徑而定。vue
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reopen
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
ps -ef|grep nginx
kill -QUIT <進程號> 或 kill -TERM <進程號>
pkill -9 nginx
Nginx做爲Web服務器, 須要定義server虛擬主機,讓這些虛擬主機去處理對於特定域名或IP地址的請求。
每一個server虛擬主機都定義了 location 指令,location 定義了對於指定的一組 URI 是如何匹配和進行處理的。java
server { listen 80; server_name www.example.com; location / { root /usr/local/www; index index.html; } }
參數說明:nginx
server { location 表達式 { } }
URL重寫是指: 當請求的URL知足事先定義好的規則時, 將跳轉/定向到某個規則,好比常見的僞靜態、301重定向、瀏覽器定向等。web
server { rewrite 規則 定向路徑 重寫類型; }
rewrite參數說明:正則表達式
$index
來表示正則裏的捕獲分組重寫類型:算法
域名跳轉: 訪問 www.aaa.com 跳轉到 www.bbb.com
server { listen 80; server_name www.aaa.com; location / { rewrite ^/$ www.bbb.com permanent ; } }
try_files是指: 按順序檢查文件是否存在,返回第一個找到的文件。若是全部的文件都找不到,會進行一個內部重定向到最後一個參數.
try_files file1 files2 ... uri
參數說明:
try_files $uri $uri/ /index.php?q=$uri&$args;
當訪問:www.example.com/test 時會依次查找,若 1.html,2.html 都不存在,最終返回 3.html
server { listen 80; server_name www.example.com; root html; index index.html; location /test { try_files /1.html /2.html /3.html; } }
當訪問:www.example.com/test 時會依次查找,若 1.html,2.html 都不存在,則跳轉到命名爲abc的location
server { listen 80; server_name www.example.com; root html; index index.html; location /test { try_files /1.html /2.html @abc; } location @abc{ rewrite ^/(.*)$ http://www.example2.com; } }
location / { # URL 匹配不到任何靜態資源,返回同一個 index.html 頁面,這個頁面就是你 app 依賴的頁面。 try_files $uri $uri/ /index.html; }
server { # 開啓gzip 壓縮 gzip on; # 設置gzip所需的http協議最低版本 (HTTP/1.1, HTTP/1.0) gzip_http_version 1.1; # 設置壓縮級別(1-9), 值越大壓縮率越高,同時消耗cpu資源也越多,建議設置在4左右 gzip_comp_level 4; # 設置壓縮的最小字節數, 頁面Content-Length獲取 gzip_min_length 1000; # 設置壓縮文件的類型 (text/html), 不建議壓縮圖片(如jpg、png自己已壓縮) gzip_types text/plain application/javascript text/css; #配置禁用gzip條件,支持正則。此處表示ie6及如下不啓用gzip(由於ie低版本不支持) gzip_disable "MSIE [1-6]\."; }
http { # 配置共享會話緩存大小,視站點訪問狀況設定 ssl_session_cache shared:SSL:10m; # 配置會話超時時間 ssl_session_timeout 10m; server { listen 443; server_name www.example.com; ssl on; # 設置長鏈接 keepalive_timeout 70; # HSTS策略 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; # 證書文件 ssl_certificate www.example.com.crt; # 私鑰文件 ssl_certificate_key www.example.com.key; # 優先採起服務器算法 ssl_prefer_server_ciphers on; # 指定SSL協議 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 定義算法 ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"; # 減小點擊劫持 add_header X-Frame-Options DENY; # 禁止服務器自動解析資源類型 add_header X-Content-Type-Options nosniff; # 防XSS攻擊 add_header X-Xss-Protection 1; } }
server { listen 80; server_name www.example.com; root html; index index.html; location /test { # 請求host proxy_set_header Host $http_host; # 請求ip proxy_set_header X-Real-IP $remote_addr; # 請求協議 proxy_set_header X-Scheme $scheme; # 代理服務器 proxy_pass http://localhost:3000; } }
當訪問http://www.example.com/test時, nginx會將請求轉發到http://localhost:3000上。
在服務器集羣中,Nginx起到一個代理服務器的角色(即反向代理),爲了不單獨一個服務器壓力過大,未來自用戶的請求轉發給不一樣的服務器。
負載均衡用於從 "upstream" 模塊定義的後端服務器列表中選取一臺服務器接受用戶的請求。
一個最基本的upstream模塊以下:
#動態服務器組, server是後端服務器,my_server是自定義的服務器組名稱。 upstream my_server { server localhost:8001; server localhost:8002; server localhost:8003; }
在upstream模塊配置完成後,要讓指定的訪問反向代理到服務器組。
server { listen 80; server_name www.example.com; root html; index index.html; location / { # 反向代理到定義好的服務器組my_server proxy_pass my_server; } }
http { upstream my_server { server localhost:8001; server localhost:8002; server localhost:8003; } server { listen 80; server_name www.example.com; root html; index index.html; location / { # 反向代理到定義好的服務器組my_server proxy_pass my_server; } } }
表示每一個請求按時間順序逐一分配到不一樣的後端服務器。
upstream my_server { server localhost:8001; server localhost:8002; }
表示在輪詢策略的基礎上指定輪詢的服務器的權重,默認爲1,權重越高分配到須要處理的請求越多。
upstream my_server { server localhost:8001 weight=1; server localhost:8002 weight=2; }
表示指定負載均衡器按照基於客戶端IP的分配方式,這個方法確保了相同的客戶端的請求一直髮送到相同的服務器,以保證session會話。這樣每一個訪客都固定訪問一個後端服務器,能夠解決session不能跨服務器的問題。
upstream my_server { ip_hash; server localhost:8001; server localhost:8002; }
備註:
表示把請求轉發給鏈接數較少的後端服務器。輪詢算法是把請求平均的轉發給各個後端,使它們的負載大體相同;可是,有些請求佔用的時間很長,會致使其所在的後端負載較高。這種狀況下,least_conn這種方式就能夠達到更好的負載均衡效果。
upstream my_server { least_conn; server localhost:8001; server localhost:8002; }
表示當前的server暫時不參與負載均衡。
upstream my_server { server localhost:8001 down; server localhost:8002; server localhost:8003; }
表示預留的備份機器。當其餘全部的非backup機器出現故障或者忙的時候,纔會請求backup機器,因 此這臺機器的壓力最輕。
upstream my_server { server localhost:8001 backup; server localhost:8002; server localhost:8003; }