1. 程式人生 > >Nginx動靜分離+proxy_cache快取

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; } }

壓力測試

完整配置

#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;
        }
    }
}