一、正向代理: 客戶端和原始服務器(origin server)之間的服務器,爲了從原始服務器取得內容,客戶端向代理髮送一個請求並指定目標(原始服務器),而後代理向原始服務器轉交請求並將得到的內容返回給客戶端。javascript
server不知道client是誰java
二、反向代理:客戶端請求服務器,中間也是通過一個代理服務器,客戶端訪問代理服務器就好像訪問目標服務器同樣。同時代理服務器將請求轉發到後端具體服務器。nginx
客戶端不知道本身具體訪問的服務器是誰後端
三、總結 https://blog.csdn.net/wnvalentin/article/details/88171847緩存
正向代理是對客戶端的代理,由客戶端設立,客戶端了解代理服務器和目標服務器,但目標服務器不瞭解真正的客戶端是誰;使用正向代理可達到 突破訪問限制、提升訪問速度、對服務器隱藏客戶端IP等目的;安全
反向代理是對服務器的代理,由服務器設立,客戶端不瞭解真正的服務器是誰,使用反向代理可達到負載均衡、保障服務端安全、對客戶端隱藏服務器IP等目的。服務器
集羣平攤請求壓力架構
負載均衡策略:併發
upstream myserver{ ip_hash; server 192.168.17.129:8000; server 192.168.17.129:8001; server 127.0.0.1:7071 down; #不參與負載均衡 server 127.0.0.1:7070 backup; #備份server 只有其餘都忙了才訪問 }
upstream myserver{ server 192.168.17.129:8000 weight 1; server 192.168.17.129:8001 weight 2; }
upstream myserver{ ip_hash; server 192.168.17.129:8000; server 192.168.17.129:8001; }
upstream myserver{ server 192.168.17.129:8000; server 192.168.17.129:8001; hash $request_uri; hash_method crc32; #hash 方法 }
upstream myserver{ server 192.168.17.129:8000; server 192.168.17.129:8001; fair; }
server模塊的配置:app
server{ listen 80; server_name 192.168.17.129; location /{ proxy_pass http://myserver; #前面upstream的名字 } }
動態頁面和靜態頁面分開部署,把動態頁面的服務器性能弄的好一些,靜態頁面的服務器能夠差一些,而且能夠設置緩存。
nginx 啓動
nginx -v版本號
nginx -s stop 關閉
nginx -s reload 從新加載配置文件
location /appImg/{ root /home/nginx; }
這個location至關於訪問服務器上的文件路徑: /home/nginx/appImg/ 。
location /appImg/{ alias /home/nginx/; }
這個alias表明了/appimg/ = /home/nginx/
設置併發數和鏈接數
鏈接數:
靜態資源:2
反向代理:4
每一個worker都維護一個線程處理請求
worker_processes設置的數量和cpu核數最好相等,每一個worker都是一個獨立的進程
3個worker
root 20789 1 0 10:19 ? 00:00:00 nginx: master process nginx nobody 22281 20789 0 10:47 ? 00:00:00 nginx: worker process nobody 22282 20789 0 10:47 ? 00:00:00 nginx: worker process nobody 22283 20789 0 10:47 ? 00:00:00 nginx: worker process root 22319 20514 0 10:48 pts/1 00:00:00 grep --color=auto nginx
1個worker
root 20789 1 0 10:19 ? 00:00:00 nginx: master process nginx nobody 22376 20789 0 10:49 ? 00:00:00 nginx: worker process root 22378 20514 0 10:49 pts/1 00:00:00 grep --color=auto nginx