1 ## Nginx 2 3 - 反向代理 4 > 反向代理方式指服務器接收internet的連接請求 而後根據請求轉化給內部網絡服務器上 5 - 負載均衡 6 1.每一個請求按時間順序逐一分配到後端服務器, 7 ``` 簡單配置 8 upstream test { 9 server localhost:8080; 10 server localhost:8081; 11 12 } 13 14 server { 15 listen 80; 16 server_name:localhost; 17 18 location / { 19 proxy_pass http://test; 20 21 } 22 } 23 24 ``` 25 26 2. 權重 ;指定輪詢機率,weight 和訪問比率成正向比,用於後端服務器鑫能不均衡狀況 27 28 ``` 29 upstream test { 30 server localhost:8080 weight=9; 31 server localhost:8081 weight=1; 32 33 } 34 //10次訪問1次8081 9次8080 35 ``` 36 37 3. ip_hash 程序請求不是無狀態時候(採用session保存數據)咱們須要一個客戶訪問一個服務器,ip_hash的每一個請求按訪問ip的 hash結果分配這樣每一個訪客固定一個後端服務器 38 39 ``` 40 upstream test { 41 ip_hash; 42 server localhost:8080 weight=9; 43 server localhost:8081 weight=1; 44 45 } 46 ``` 47 48 4. fair (第三方) 按後端響應時間來分配請求 響應時間短的優先分配 49 5. url_hash (第三方) 按訪問url的hash結果來分配請求使每一個url定向到同一個後端服務器 在upstream中加入hash語句 server 語句中不能寫入weight 等其餘參數 hash_method是使用hash的算法 50 51 ``` 52 upstream test { 53 hash: $request_uri; 54 hash_method crc32; 55 server localhost:8080 ; 56 server localhost:8081 ; 57 58 } 59 ``` 60 61 - HTPP服務器 (動靜分離) 62 Nginx自己也是一個資源服務器,只有在靜態資源的時候能夠使用Nginx來作代理 同時實現動靜分離 63 64 ``` 65 server { 66 listen 80; 67 server_name:localhost; 68 69 location / { 70 root e:\html; 71 index index.html; 72 } 73 } 74 ``` 75 > 訪問時會默認訪問E盤html目錄下index.html 頁面 76 77 78 ``` 79 server { 80 listen 80; 81 server_name:localhost; 82 83 location / { 84 root e:\html; 85 index index.html; 86 } 87 88 localhost ~ \.(gif|jpg|jpge)$ { 89 root e:\static; 90 } 91 } 92 ``` 93 > 全部靜態文件都有nginx 處理,存放目錄爲html 94 95 - 正向代理