最近項目引入反向 Proxy和緩衝,熟悉了一下Squid、Apache、Valish、Nginx,根據項目實際進行選擇,客觀來說,採用Linux系統部署最好,也沒有什麼難度,但實際情況必須採用Windows系統(本著方案要結合現實的原則,研究要以Windows平台為主)。
Nginx配置:
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #::cst:: send_timeout 300s; #server_names_hash_bucket_size 128; #指定伺服器名稱雜湊表的框大小 #client_header_buffer_size 1k; #設定讀取用戶端要求標頭部的緩衝容量。 large_client_header_buffers 4 32k; #以上兩個是設定用戶端請求的Header頭緩衝區大小,對於,cookie內容較大的請求,應增大改值。(400或414錯誤) client_max_body_size 300m; #允許用戶端請求的最大單檔案位元組數 client_body_buffer_size 128k; #設定讀取用戶端請求本文的緩衝容量。緩衝大小預設等於兩塊記憶體頁的大小,在x86平台、其他32位平台和x86-64平台,這個值是8K。在其他64位平台,這個值一般是16K。 proxy_connect_timeout 300s; #nginx跟後端伺服器連線逾時時間(代理連線逾時) proxy_read_timeout 300s; #串連成功後,後端伺服器回應時間(代理接收逾時) proxy_send_timeout 300s; #後端伺服器資料回傳時間(代理髮送逾時) proxy_ignore_client_abort on; #不允許代理端主動關閉串連 #::cst::end sendfile on; #tcp_nopush on; #開啟或者關閉nginx在FreeBSD上使用TCP_NOPUSH通訊端選項, 在Linux上使用TCP_CORK通訊端選項。 選項僅在使用sendfile的時候才開啟。 keepalive_timeout 75s; #tcp_nodelay on; #開啟或關閉nginx使用TCP_NODELAY選項的功能。 這個選項僅在將串連轉變為長串連的時候才被啟用。 #::cst:: gzip on; gzip_min_length 20; gzip_buffers 32 4k; gzip_http_version 1.0; gzip_proxied any; #enables compression for all proxied requests.前端是squid的情況下要加此參數,否則squid上不緩衝gzip檔案 gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary off; #Enables or disables emitting the “Vary: Accept-Encoding” response header field if the directives gzip, gzip_static, or gunzip are active. upstream tg_server { server 192.168.100.253:7573; } server { listen 7579; server_name 192.168.100.253; #charset koi8-r; #access_log logs/host.access.log main; #::cst:: location ~ .*\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm|pdf)(.*|$) { #如果後端的伺服器返回502、504、執行逾時等錯誤,自動將請求轉寄到upstream負載平衡池中的另一台伺服器,實現容錯移轉。 #proxy_next_upstream http_502 http_504 error timeout invalid_header; #proxy_cache cache_tg_one; #進行緩衝,使用Web緩衝區cache_tg_one #proxy_cache_valid 200 304 12h; #對不同的HTTP狀態代碼設定不同的緩衝時間 #roxy_cache_valid 301 302 1m; #proxy_cache_valid any 1m; #proxy_cache_key $scheme$proxy_host$uri$is_args$args; #以網域名稱、URI、參數組合成Web緩衝的Key值,Nginx根據Key值雜湊,儲存緩衝內容到二級緩衝目錄內 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; #proxy_set_header Accept-Encoding ''; #要求標頭的值為空白,那麼這個要求標頭將不會傳送給後端伺服器 #proxy_ignore_headers "Cache-Control" "Expires"; #這段配置加上後,proxy_cache就能支援後台設定的expires。 #驗證是否緩衝 add_header X-Cache "Hit from in253.ngnix-cache.jjcj.com"; proxy_pass http://tg_server; expires 7d; } #副檔名以aspx|asmx|ashx結尾的Live App程式不緩衝。 location ~ .*\.(aspx|asmx|ashx)(.*|$) { proxy_pass http://tg_server; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Real-IP $remote_addr; #proxy_ignore_headers "Cache-Control" "Expires"; #這段配置加上後,proxy_cache就能支援後台設定的expires。 #驗證是否緩衝 add_header X-Cache "Miss from in253.ngnix-cache.jjcj.com"; } #::cst::end location / { proxy_pass http://tg_server; #proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}