反向代理,是指用戶經過同一服務器訪問服務器後端各被代理服務器的訪問方式。(詳見百度百科 https://baike.baidu.com/item/反向代理/7793488 )
css
一、實現外網用戶對內網服務器的訪問,相對保護內網服務器安全及節約網絡資源
二、增長緩存,緩解對內網服務器訪問的壓力
三、經過負載均衡提升業務處理能力及高可用性
html
### 代理參數,設置頭信息 cat>conf/conf.d/proxy.conf<<EOF proxy_set_header Host $host:$server_port; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; ### websocket 支持 #proxy_http_version 1.1; #proxy_set_header Upgrade $http_upgrade; #proxy_set_header Connection "upgrade"; EOF ## 添加到nginx.conf 中 sed -r -i "/include conf./i\ include proxy.conf'" nginx.conf cat > conf/conf.d/8081.conf<<EOF upstream tomcatserver1 { server 172.0.0.49: 8081; } server { listen 80; server_name 8081.max.com; access_log logs/8081.access.log main; location / { proxy_redirect off; proxy_pass http://tomcatserver1; } } EOF systemctl restart nginx
如上配置,一個最基礎的反向代理環境就搭建好了
nginx
##配置緩存參數 mkdir -p /data/nginx-temp /data/nginx-cache chown -R nginx:nginx /data/nginx-temp /data/nginx-cache cat >proxy_cache.conf<<EOF proxy_temp_path /data/nginx-temp; #緩存臨時文件路徑 proxy_cache_path /data/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50m inactive=1m; #緩存保存的路徑 EOF ## 添加到nginx.conf 中 sed -r -i "/include conf./i\ include proxy_cache.conf;" nginx.conf
proxy_cache_path 參數說明:web
- levels 指定該緩存空間有兩層hash目錄,第一層目錄爲1個數字或者字母,第二層爲2個數字或者字母
- keys_zone 指的是緩存空間名稱。 20m 爲內存緩存空間大小
- max_size 指的是緩存文件能夠佔用的最大空間。
- inactive 指的是若是一個緩存文件多長時間不被訪問,就會被刪除。(天:d、秒:s、分:m)
##靜態文件緩存配置 cat > conf/conf.d/8081.conf upstream tomcatserver1 { server 172.0.0.49: 8081; } server { listen 80; server_name 8081.max.com; access_log logs/8081.access.log main; location / { proxy_redirect off; proxy_pass http://tomcatserver1; } location ~.*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) { proxy_redirect off; #[關閉跳轉] proxy_cache nginx-cache; #[緩存的空間 -- proxy_cache.conf 中定義的 ] proxy_cache_valid 200 302 1h; #[不一樣http狀態緩存時間不一樣] proxy_cache_valid 301 1d; proxy_cache_valid any 1m; ### 強制緩存後端文件,忽略後端服務器的相關頭信息 proxy_ignore_headers Set-Cookie Cache-Control; proxy_hide_header Cache-Control; proxy_hide_header Set-Cookie; ### expires 30d; #[告訴瀏覽器緩存有效期-- 30天內能夠直接訪問瀏覽器緩存] proxy_passhttp://static; } } EOF systemctl restart nginx
負載方式 | 負載說明 |
---|---|
Round‑Robin(默認) | 接收到的請求按照順序逐一分配到不一樣的後端服務器,即便在使用過程當中,某一臺後端服務器宕機,nginx會自動將該服務器剔除出隊列,請求受理狀況不會受到任何影響。 這種方式下,能夠給不一樣的後端服務器設置一個權重值(weight),用於調整不一樣的服務器上請求的分配率;權重數據越大,被分配到請求的概率越大;該權重值,主要是針對實際工做環境中不一樣的後端服務器硬件配置進行調整的。 |
Least Connections | 下一次請求選擇後端最小連接的服務器 |
ip_hash | 使用IPv4 地址的前3個字節或者IPv6的整個地址用來計算的哈希值進行匹配,這樣的算法下一個固定ip地址的客戶端總會訪問到同一個後端服務器。 |
hash | 經過用戶端定義的關鍵詞,如文本,變量或二者組合進行標記分類,並將同一分類的請求始終請求到一個後端服務器,適合後端是cache 的場景。 |
upstream cluster { server a weight=5 max_fails=1 fail_timeout=10s; server b weight=1; server c weight=1; server d weight=5 max_fails=1 fail_timeout=10s backup; } server { listen 80; location / { proxy_pass http://cluster; } }
按照上述配置,Nginx每收到7個客戶端的請求,會把其中的5個轉發給後端a,把其中的1個轉發給後端b,把其中的1個轉發給後端c。算法
server指令支持以下參數後端
- weight = number 設置當前服務器的權重, 默認爲 1.
- max_fails = numbe 默認狀況下, 失敗次數是 1.0爲關閉這個服務器.
- fail_timout = number 超時時間爲 10 秒.
- max_conns=number
- backup 標識這個服務器是個備份服務器,即當集羣中服務器都失效時啓用該服務器
- down 標識這個服務器是個無效的服務器.
upstream cluster { least_conn; server a weight=5 max_fails=1 fail_timeout=10s; server b weight=1; server c weight=1; server d weight=5 max_fails=1 fail_timeout=10s backup; } server { listen 80; location / { proxy_pass http://cluster; } }
upstream cluster { ip_hash; server a weight=5 max_fails=1 fail_timeout=10s; server b weight=1; server c weight=1; server d weight=5 max_fails=1 fail_timeout=10s backup; } server { listen 80; location / { proxy_pass http://cluster; } }
upstream backend { hash $request_uri consistent; server backend1.example.com; server backend2.example.com; }
location /path/ { proxy_pass http://172.0.0.14:8080/path/; }
location /baidu/ { proxy_set_header Host www.baidu.com; proxy_pass https://www.baidu.com/; }
server { listen 80; server_name api.yourdomain.com; location / { rewrite ^ /(.*) https://api.yourdomain.com/$1 permanent; break; } error_page 497 https://$host:$server_port$request_uri; } server { listen 443 ssl; server_name api.yourdomain.com; access_log logs/ssl_api_access.log; ssl on; ssl_certificate conf.d/certs/api.yourdomain.com_bundle.crt; ssl_certificate_key conf.d/certs/api.yourdomain.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置 ssl_prefer_server_ciphers on; location / { proxy_pass http://tomcat_servers/$2; } location /v1.0/ { proxy_pass http://tomcat_servers/v1.0/; proxy_cookie_path /v1.0/ /; proxy_redirect off; } }