å©ç¨Nginxåå代çåçï¼å®ç°é群æå¡å¨ç¬é´æ
é转移ï¼çç¨äºç产ç¯å¢ä¸ç»¼å设置çä¾å
#注ï¼proxy_temp_pathåproxy_cache_pathæå®çè·¯å¾å¿
é¡»å¨åä¸ååº
proxy_temp_path /data0/proxy_temp_dir;
#设置Webç¼ååºå称为cache_oneï¼å
åç¼å空é´å¤§å°ä¸º200MBï¼1天没æ被访é®çå
容èªå¨æ¸
é¤ï¼ç¡¬çç¼å空é´å¤§å°ä¸º30GBã
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#轮询æå¡å¨ï¼weight为æå¡å¨æéï¼ä¸è®¿é®é¢çææ£æ¯ï¼max_failsæ大è¶
æ¶æ¬¡æ°ï¼fail_timeoutæå¡å¨ä»£ççå¬è¶
æ¶æ¶é´
upstream backend_server {
server 192.168.203.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.203.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.203.45:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name
www.yourdomain.com 192.168.203.42;
index index.html index.htm;
root /data0/htdocs/www;
location /
{
#å¦æå端çæå¡å¨è¿å502ã504ãæ§è¡è¶
æ¶çé误ï¼èªå¨å°è¯·æ±è½¬åå°upstreamè´è½½åè¡¡æ± ä¸çå¦ä¸å°æå¡å¨ï¼å®ç°æ
é转移ã
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对ä¸åçHTTPç¶æç 设置ä¸åçç¼åæ¶é´
proxy_cache_valid 200 304 12h;
#以ååãURIãåæ°ç»åæWebç¼åçKeyå¼ï¼Nginxæ ¹æ®Keyå¼åå¸ï¼åå¨ç¼åå
容å°äºçº§ç¼åç®å½å
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass
http://backend_server; expires 1d;
}
}
Nginxåå代çé
ç½®åæ°éä¹ï¼
1.proxy_set_header(设å®header)
2.proxy_hide_header(éèheader)
3.proxy_pass_header(éè¿header)
4.proxy_connect_timeout(代çè¿æ¥è¶
æ¶)
5.proxy_send_timeout(代çåéè¶
æ¶)
6.proxy_read_timeout(代çæ¥æ¶è¶
æ¶)
7.proxy_temp_file_write_size(设å®ç¼åæ件夹大å°)
8.proxy_buffer_size(代çç¼å²å¤§å°)
9.proxy_buffers (代çç¼å²)
10.proxy_busy_buffers_size(é«è´è·ä¸ç¼å²å¤§å°)
11.proxy_ignore_client_abort(ä¸å
许代ç端主å¨å
³éè¿æ¥)
ä¸é¢å°±åæ¥ä»ç»åºäºNginxåå代ççupstream对æå¡è¯·æ±è½¬åä¸åé
5ç§æ¹å¼ï¼å®é
çæç¯å¢ç»¼å设置ï¼ä¸ºäºä¾¿äºè¯´æé®é¢åä¸åæ¹å¼æ¥è¯´æï¼nginxåå代çå®é
çæç¯å¢çåºç¨ï¼è¯·åèãå¦ä½è®¾ç½®nginxåå代çå®ç°æå¡å¨ç¬é´æ
é转移ãæç« å¼ç¯é¨åçproxy.confé
ç½®ã
nginxçupstreamç®åæ¯æ5ç§æ¹å¼çåé
1ã轮询ï¼é»è®¤ï¼
æ¯ä¸ªè¯·æ±ææ¶é´é¡ºåºéä¸åé
å°ä¸åçå端æå¡å¨ï¼å¦æå端æå¡å¨downæï¼è½èªå¨åé¤ã
2ãweight
æå®è½®è¯¢å çï¼weightå访é®æ¯çææ£æ¯ï¼ç¨äºå端æå¡å¨æ§è½ä¸åçæ
åµã
3ãip_hash
æ¯ä¸ªè¯·æ±æ访é®ipçhashç»æåé
ï¼è¿æ ·æ¯ä¸ªè®¿å®¢åºå®è®¿é®ä¸ä¸ªå端æå¡å¨ï¼å¯ä»¥è§£å³sessionçé®é¢ã
upstream bakend {
ip_hash;
server 192.168.203.14:88;
server 192.168.203.15:80;
}
4ãfairï¼ç¬¬ä¸æ¹ï¼
æå端æå¡å¨çååºæ¶é´æ¥åé
请æ±ï¼ååºæ¶é´ççä¼å
åé
ã
upstream backend {
server 192.168.203.14:88;
server 192.168.203.15:80;
fair;
}
5ãurl_hashï¼ç¬¬ä¸æ¹ï¼
æ访é®urlçhashç»ææ¥åé
请æ±ï¼ä½¿æ¯ä¸ªurlå®åå°åä¸ä¸ªå端æå¡å¨ï¼å端æå¡å¨ä¸ºç¼åæ¶æ¯è¾ææã
ä¾ï¼å¨upstreamä¸å å
¥hashè¯å¥ï¼serverè¯å¥ä¸ä¸è½åå
¥weightçå
¶ä»çåæ°ï¼hash_methodæ¯ä½¿ç¨çhashç®æ³
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
upstream bakend{
#å®ä¹è´è½½å衡设å¤çIpå设å¤ç¶æ
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
å¨éè¦ä½¿ç¨è´è½½åè¡¡çserverä¸å¢å ï¼
proxy_pass
http://bakend/; æ¯ä¸ªè®¾å¤çç¶æ设置为:
1.down 表示ååçserverææ¶ä¸åä¸è´è½½
2.weight é»è®¤ä¸º1.weightè¶å¤§ï¼è´è½½çæéå°±è¶å¤§ã
3.max_fails ï¼å
许请æ±å¤±è´¥ç次æ°é»è®¤ä¸º1.å½è¶
è¿æ大次æ°æ¶ï¼è¿åproxy_next_upstream 模åå®ä¹çé误
4.fail_timeout:max_fails次失败åï¼æåçæ¶é´ã
5.backupï¼ å
¶å®ææçébackupæºå¨downæè
å¿çæ¶åï¼è¯·æ±backupæºå¨ãæ以è¿å°æºå¨ååä¼æè½»ã
nginxæ¯æåæ¶è®¾ç½®å¤ç»çè´è½½åè¡¡ï¼ç¨æ¥ç»ä¸ç¨çserveræ¥ä½¿ç¨ã
client_body_in_file_only 设置为On å¯ä»¥è®²client
postè¿æ¥çæ°æ®è®°å½å°æ件ä¸ç¨æ¥ådebug
client_body_temp_path 设置记å½æ件çç®å½ å¯ä»¥è®¾ç½®æå¤3å±ç®å½
location 对URLè¿è¡å¹é
.å¯ä»¥è¿è¡éå®åæè
è¿è¡æ°ç代ç è´è½½åè¡¡