Nginx 缓存服务配置教程,nginx缓存配置教程


# wgethttp://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz //清缓存模块


# tar zxvpf ngx_cache_purge-1.3.tar.gz -C ../software/


# cd /usr/local/src/software/nginx-1.0.2


# ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx-1.0.2 --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_cache_purge-1.3


# mkdir -p /www/nginx/proxy_temp_path


# mkdir -p /www/nginx/proxy_cache_path


nginx.conf配置文件:


user nobody nobody;


worker_processes 4;


error_log logs/error.log crit;


pid logs/nginx.pid;


worker_rlimit_nofile 65535;


events {


use epoll;


worker_connections 65535;


}


http {


server_tokens off;


include mime.types;


default_type application/octet-stream;


server_names_hash_bucket_size 128;


sendfile on;


tcp_nopush on;


tcp_nodelay on;


keepalive_timeout 65;


gzip on;


gzip_min_length 1k;


gzip_buffers 4 16k;


gzip_http_version 1.0;


gzip_comp_level 2;


gzip_types text/plain application/x-javascript text/css application/xml;


gzip_vary on;

upstream mysrv {


server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;


server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;


}

include vhost/*.conf;


}

aaa_example_com.conf配置文件:


server {


listen 80;


server_name aaa.example.com;


index index.php index.html index.htm index.shtml;

log_format proxy '$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|'


' $status| $body_bytes_sent| $bytes_sent| $http_referer|'


' $http_user_agent| $upstream_response_time| $msec| $request_time';


access_log logs/aaa_access.log proxy;

location /


{


proxy_passhttp://mysrv;


proxy_set_header Host $host;


proxy_set_header X-Forwarded-For $remote_addr;


}


location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$


{


proxy_passhttp://mysrv;


include proxy.conf;


}


location ~ /purge(/.*)


{


allow 127.0.0.1;


allow 192.168.1.0/24;


deny all;


proxy_cache_purge cache_one $host$1$is_args$args;


}


}

proxy.conf配置文件:


proxy_redirect off;


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


client_max_body_size 50m; //允许客户端请求的最大单个文件字节数


client_body_buffer_size 256k; //缓冲区代理缓冲客户端请求的最大字节数


proxy_connect_timeout 30; //连接后端服务器超时时间


proxy_send_timeout 30; //后端服务器发送数据超时时间


proxy_read_timeout 60; //后端服务器响应请求超时时间


proxy_buffer_size 4k; //代理请求缓存区大小


proxy_buffers 4 32k;


proxy_busy_buffers_size 64k; //系统繁忙时可申请的proxy_buffers大小


proxy_temp_file_write_size 64k; //proxy缓存临时文件的大小


proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; //故障转移


proxy_max_temp_file_size 128m;


proxy_temp_path /www/nginx/proxy_temp_path;


proxy_cache_path /www/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g; //设置web缓存区名称为cache_one,内存缓存空间为200m,自动清除超过1天没有被访问的缓存数据,硬盘缓存空间为1g


proxy_cache cache_one; //使用web缓存区cache_one


proxy_cache_valid 200 304 12h;


proxy_cache_valid 301 302 1m;


proxy_cache_valid any 1m;


proxy_cache_key $host$uri$is_args$args; //设置web缓存的key值,nginx根据key值md5哈希存储缓存

proxy_set_header指令用于在向反向代理的后端web服务器发起请求时添加指定Header头信息,当后端web服务器上有多个基于域名的虚拟主机时,要通过添加Header头信息Host,来指定请求的域名,这样后端web服务器才能识别该反向代理访问请求由哪个虚拟主机来处理。

相关内容

    暂无相关文章