ngix缓存设置详解

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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

    #::cst::
    send_timeout 300s;
    #server_names_hash_bucket_size 128;     #指定服务器名称哈希表的框大小
    #client_header_buffer_size 1k;         #设置读取客户端请求头部的缓冲容量。
    large_client_header_buffers 4 32k;     #以上两个是设定客户端请求的Header头缓冲区大小,对于,cookie内容较大的请求,应增大改值。(400或414错误)
    client_max_body_size 300m;                #允许客户端请求的最大单文件字节数
    client_body_buffer_size 128k;            #设置读取客户端请求正文的缓冲容量。缓冲大小默认等于两块内存页的大小,在x86平台、其他32位平台和x86-64平台,这个值是8K。在其他64位平台,这个值一般是16K。
    
    proxy_connect_timeout 300s;              #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_read_timeout    300s;              #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_send_timeout    300s;              #后端服务器数据回传时间(代理发送超时)
    proxy_ignore_client_abort on;           #不允许代理端主动关闭连接
    #::cst::end

    sendfile       on;
    #tcp_nopush     on;  #开启或者关闭nginx在FreeBSD上使用TCP_NOPUSH套接字选项, 在Linux上使用TCP_CORK套接字选项。 选项仅在使用sendfile的时候才开启。
    keepalive_timeout  75s;
    #tcp_nodelay on;  #开启或关闭nginx使用TCP_NODELAY选项的功能。 这个选项仅在将连接转变为长连接的时候才被启用。


    #::cst::
    gzip  on;
    gzip_min_length  20;
    gzip_buffers     32 4k;
    gzip_http_version 1.0;
    gzip_proxied    any;  #enables compression for all proxied requests.前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
    gzip_comp_level 6;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary   off;   #Enables or disables emitting the “Vary: Accept-Encoding” response header field if the directives gzip, gzip_static, or gunzip are active.

    upstream  tg_server {
        server 192.168.100.253:7573;
    }

    server {
        listen       7579;
        server_name  192.168.100.253;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #::cst::
        location ~ .*\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm|pdf)(.*|$)
        {
            #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
            #proxy_next_upstream http_502 http_504 error timeout invalid_header;

            #proxy_cache cache_tg_one;                  #进行缓存,使用Web缓存区cache_tg_one
            #proxy_cache_valid 200 304 12h;              #对不同的HTTP状态码设置不同的缓存时间
            #roxy_cache_valid 301 302 1m;
            #proxy_cache_valid any 1m;
            #proxy_cache_key $scheme$proxy_host$uri$is_args$args;  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
            
            proxy_set_header  Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            #proxy_set_header Accept-Encoding '';   #请求头的值为空,那么这个请求头将不会传送给后端服务器
            
            #proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。

            #验证是否缓存
            add_header X-Cache "Hit from in253.ngnix-cache.jjcj.com";
            
            proxy_pass http://tg_server;
            expires  7d;
        }

        #扩展名以aspx|asmx|ashx结尾的动态应用程序不缓存。
        location ~ .*\.(aspx|asmx|ashx)(.*|$) {
            proxy_pass http://tg_server;
            proxy_set_header  Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Real-IP $remote_addr;
            #proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。
            #验证是否缓存
            add_header X-Cache "Miss from in253.ngnix-cache.jjcj.com";
        }
        #::cst::end

        location / {
            proxy_pass http://tg_server;
            #proxy_set_header  Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            #proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

猜你喜欢

转载自javakill.iteye.com/blog/2084339