Nginx动静分离+proxy_cache缓存

nginx的动静分离和proxy_cache缓存可以同时设置,感觉在集群部署时可能效果更好。

一.http模块增加缓存设置

    proxy_temp_path /etc/nginx/proxy_temp;   
    proxy_cache_path /etc/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g;
    inactive=5d max_size=1g;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

二.Server模块

动静分离和缓存的结合(经测试研究,nginx如果做了动静分离,自带的proxy_cache模块来无法实现nginx缓存自身代理的静态页面,所以再加一个server端,方便缓存配置和集群部署)

    server {
        listen       80;
        server_name  www.nginx2.com;
        #静态处理交给nginx
        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|css|js)$ {
            proxy_cache cache_one;
            proxy_cache_valid 200 304 302 2d;
            proxy_cache_valid any 1d;
            proxy_cache_key $host$uri$is_args$args;
            add_header X-Cache '$upstream_cache_status from $host';
            proxy_pass http://127.0.0.1:8088;
            expires 30d;
        }
        #动态文件不处理
        location ~ .*$ {
            proxy_pass http://127.0.0.1:8080;
            proxy_cookie_path /nginx1/ /;
        }
}
    server{
        listen 8088;
        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|css|js)$ {
            root /opt/apache-tomcat-7.0.64/webapps/nginx1;
            expires 30d;
        }
    }

压力测试

#yum install httpd-tools -y
#ab -n 3000 -c 1000 url(如http://www.nginx2.com/index.jsp)

完整配置

#user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;

pid /var/run/nginx.pid;


events {
    use epoll;
    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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip 压缩传输
    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-javascripttext/css application/xml;
    gzip_vary on;
    #配置代理参数
    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;
    proxy_connect_timeout 90;
    proxy_read_timeout 90;
    proxy_send_timeout 90;
    proxy_buffer_size 4k;
    #缓存配置
    proxy_temp_file_write_size 264k;
    proxy_temp_path /mnt/nginx_temp;
    proxy_cache_path /mnt/nginx_cache levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=400m;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;  

    server {
        listen       80;
        server_name  www.nginx2.com;
        #静态文件nginx处理并缓存
        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|css|js)$ {
            proxy_cache cache_one;
            proxy_cache_valid 200 304 302 2d;
            proxy_cache_valid any 1d;
# 以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
            proxy_cache_key $host$uri$is_args$args; 
            add_header X-Cache '$upstream_cache_status from $host'; 
            proxy_pass http://127.0.0.1:8088;
            expires 30d;
        }
        #其他页面反向代理到tomcat容器
        location ~ .*$ {
            proxy_pass http://127.0.0.1:8080;
            proxy_cookie_path /nginx1/ /;
        }


    }

    server{
        listen 8088;
        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|css|js)$ {
            #所有静态文件直接读取硬盘
            root /opt/apache-tomcat-7.0.64/webapps/nginx1;
            expires 30d;
        }
    }
}

你可能感兴趣的:(web)