歡迎您光臨本站 註冊首頁

nginx緩存配置

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

nginx緩存配置

在測試環境下搭建了一台nginx加兩台tomcat的負載均衡配置,測試后發現效果一般,現在想配置下nginx的緩存,再重新測試,我的nginx.conf配置如下:
user  www www;
worker_processes 4;
error_log  /usr/local/nginx/logs/nginx_error.log  debug;
pid    /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events
{
    use epoll;
    worker_connections 65535;
}
http
{
    include       mime.types;
    default_type  application/octet-stream;
    #charset  gb2312;
   
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    sendfile on;
    tcp_nopush     on;
    keepalive_timeout 60;
    tcp_nodelay on;

    fastcgi_temp_path  /opt/proxy_temp_path;
    fastcgi_cache_path  /opt/proxy_cache_path levels=1:2 keys_zone=cache_one:200m  inactive=1d max_size=10g;
   
    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;
    gzip_proxied any;

    upstream  tomcats  {
        server   192.168.131.57:8081;
        server   192.168.131.56:8081;
    #    server   192.168.131.61:8080;
    }
    server
    {
        listen  81;
        server_name localhost;
        proxy_redirect off;
     
  
      location / {
                proxy_pass   http://tomcats;
                expires 12h;
        }
     #擴展名以.gif、.jpg、.css等結尾的靜態文件緩存。

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

        {

        fastcgi_cache cache_one;  
        fastcgi_cache_valid 200 304 12h;  
        fastcgi_cache_valid 301 302 1m;
        fastcgi_cache_valid any 1m;
        fastcgi_cache_key $host$uri$is_args$args;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        add_header X-Cache "HIT from cache_test";
        add_header Age "1";
        proxy_set_header Accept-Encoding "none";
        proxy_ignore_headers "Cache-Control" "Expires";
        proxy_pass http://tomcats;
        expires  1h;
        }

  #擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。

        location ~ .*\.(php|cgi)?$  {
        proxy_set_header  Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://tomcats;
        }


        location ~ ^/NginxStatus {
        stub_status on;
        access_log off;
        if (-d $request_filename){
        rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
        }
        }
   
        location ~ ^/(WEB-INF)/ {
        deny all;
        }
      
   #設置只允許指定的IP或IP段才可以清除URL緩存。
        location ~ /purge(/.*)  {
        allow            127.0.0.1;
        allow            192.168.0.0/16;
        allow             all;
        }   

        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }

        log_format  access  '$remote_addr - $remote_user [$time_local] $request '
                   '"$status" $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  /usr/local/nginx/logs/access.log  access;
        client_max_body_size     300m;
        client_body_buffer_size  512k;
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;
        proxy_buffer_size        16k;
        proxy_buffers            4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;
  
    }
}

我用這個加了緩存的nginx配置,重新測試,效果和之前沒加緩存的差不多,是不是我的緩存配置失敗了呢?還有,如果通過緩存去訪問了,有沒有相關日誌能看到呢?

[火星人 ] nginx緩存配置已經有471次圍觀

http://coctec.com/docs/service/show-post-16785.html