1. 程式人生 > >nginx實戰(四)反向代理配置緩存及負載均衡

nginx實戰(四)反向代理配置緩存及負載均衡

cer zone ofo domain 針對 臨時文件 地址 ike ipv6

前言

反向代理,是指用戶通過同一服務器訪問服務器後端各被代理服務器的訪問方式。(詳見百度百科 https://baike.baidu.com/item/反向代理/7793488 )
?

nginx 反向代理的應用場景

1、實現外網用戶對內網服務器的訪問,相對保護內網服務器安全及節約網絡資源
2、增加緩存,緩解對內網服務器訪問的壓力
3、通過負載均衡提高業務處理能力及高可用性
?

訪問內網配置

### 代理參數,設置頭信息
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

如上配置,一個最基礎的反向代理環境就搭建好了
?
?

緩存配置

##配置緩存參數
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 參數說明:

  • 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

?

nginx 負載均衡調度算法及配置

?

負載方式 負載說明
Round?Robin(默認) 接收到的請求按照順序逐一分配到不同的後端服務器,即使在使用過程中,某一臺後端服務器宕機,nginx會自動將該服務器剔除出隊列,請求受理情況不會受到任何影響。 這種方式下,可以給不同的後端服務器設置一個權重值(weight),用於調整不同的服務器上請求的分配率;權重數據越大,被分配到請求的幾率越大;該權重值,主要是針對實際工作環境中不同的後端服務器硬件配置進行調整的。
Least Connections 下一次請求選擇後端最小鏈接的服務器
ip_hash 使用IPv4 地址的前3個字節或者IPv6的整個地址用來計算的哈希值進行匹配,這樣的算法下一個固定ip地址的客戶端總會訪問到同一個後端服務器。
hash 通過用戶端定義的關鍵詞,如文本,變量或兩者組合進行標記分類,並將同一分類的請求始終請求到一個後端服務器,適合後端是cache 的場景。

weight輪詢

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 標識這個服務器是個無效的服務器.

Least Connections

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;
    }
}

ip_hash

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;
    }
}

hash

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/;
}

強制http轉https訪問

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;
        }
}

nginx實戰(四)反向代理配置緩存及負載均衡