利用nginx做cache處事器
需求就是緩存android的軟件包,后綴名是apk。話不多說,直接上設(shè)置,供參考:
a-->nginx.conf
user www www;
worker_processes 8;
error_log /data/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
#apk 文件范例
#default_type application/vnd.android.package-archive;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
client_body_buffer_size 512k;
#跟后端處事器毗連的超時時間_提倡握手等待響應(yīng)超時時間
proxy_connect_timeout 600;
#毗連樂成后_等待后端處事器響應(yīng)的時間_其實已經(jīng)進(jìn)入后端的列隊之中等待處理懲罰
proxy_read_timeout 600;
#后端處事器數(shù)據(jù)回傳時間_就是在規(guī)按時間內(nèi)后端處事器必需傳完所有數(shù)據(jù)
proxy_send_timeout 600;
#署理請求緩存區(qū)_這個緩存區(qū)間會生存用戶的頭信息以供Nginx舉舉措則處理懲罰_一般只要能生存下頭信息即可
proxy_buffer_size 16k;
#同上 匯報Nginx生存單個用的幾個Buffer最大用多大空間
proxy_buffers 4 64k;
#假如系統(tǒng)很忙的時候可以申請更大的proxy_buffers 官方推薦*2
proxy_busy_buffers_size 128k;
#proxy緩存姑且文件的巨細(xì)
proxy_temp_file_write_size 128k;
gzip on;
gzip_proxied expired no-cache no-store private auth;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
#log_format access '$remote_addr - $remote_user [$time_local] '
# '"$request" $status $body_bytes_sent '
# '"$http_referer" "$http_user_agent" '
# '$host $request_time $http_x_forwarded_for';
#access_log /data/logs/http.a.log;
#error_log /data/logs/http.e.log;
include vhosts/cache.peiqiang.net.conf;
}
upstream source_site {
server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s;
server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s;
}
b-->cache.peiqiang.net.conf
#用于指定當(dāng)?shù)啬看蝸砭彌_較大的署理請求
proxy_temp_path /data/soft/temp;
#配置web緩存區(qū)名為cache_one,內(nèi)存緩存空間巨細(xì)為12000M,自動排除高出15天沒有被會見過的緩存數(shù)據(jù),硬盤緩存空間巨細(xì)200g
proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g;
server {
listen 80;
server_name cache.peiqiang.net;
access_log /data/logs/a.log;
error_log /data/logs/e.log notice;
# PHP Scripts is NOT allowed within this site!
location ~* \.(php|php5|jsp|asp|aspx)$ {
deny all;
}
location / {
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
#反向署理,會見后端內(nèi)容源處事器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
}
location ~* .*\.(apk)$ {
error_page 302 404 = @fallback;
#假如后端的處事器返回500、502、503、504執(zhí)行超時等錯誤、自動將請求轉(zhuǎn)發(fā)到upstream負(fù)載平衡池中的另一臺處事器,實現(xiàn)妨礙轉(zhuǎn)移
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
#利用web緩存區(qū)cache_one
proxy_cache cache_one;
#對差異的HTTP狀態(tài)碼緩存配置差異的緩存時間
proxy_cache_valid 200 304 12h;
#配置Web緩存的Key值,Nginx按照Key值md5哈希存儲緩存,這里按照"域名、URI、參數(shù)"組合成key
proxy_cache_key $uri$is_args$args;
#反向署理,會見后端內(nèi)容源處事器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://source_site;
expires 1d;
}
location @fallback {
rewrite ^ $scheme://apke.peiqiang.net$uri redirect;
expires -1;
}
}