安哥网络 发表于 2013-12-11 19:22:06

nginx 反向代理做cache配置

nginx 的cache功能需要第三方模块的支持:ngx_cache_purge
下载地址:http://labs.frickle.com 或 https://github.com/FRiCKLE/ngx_cache_purge/

view plaincopy


[*]wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
[*]tar -zxvf ngx_cache_purge-1.3.tar.gz
[*]#nginx的源目录与ngx_cache_purge同级,现在需要重新编译nginx
[*]cd nginx-1.0.5
[*]sudo ./configure --add-module=../ngx_cache_purge-1.3 --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx   
[*]sudo make
[*]sudo make install

修改nginx的config文件 view plaincopy


[*]cd /usr/local/nginx/conf
[*]sudo gedit nginx.conf


view plaincopy


[*]#usernobody;
[*]worker_processes1;
[*]
[*]
[*]#error_loglogs/error.log;
[*]#error_loglogs/error.lognotice;
[*]#error_loglogs/error.loginfo;
[*]
[*]
[*]#pid      logs/nginx.pid;
[*]
[*]
[*]
[*]
[*]events {
[*]    worker_connections1024;
[*]}
[*]
[*]
[*]
[*]
[*]http {
[*]    include       mime.types;
[*]    default_typeapplication/octet-stream;
[*]
[*]
[*]    #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
[*]    #                  '$status $body_bytes_sent "$http_referer" '
[*]    #                  '"$http_user_agent" "$http_x_forwarded_for"';
[*]
[*]
[*]    #access_loglogs/access.logmain;
[*]
[*]
[*]    #sendfile      on;
[*]    #tcp_nopush   on;
[*]
[*]
[*]    #keepalive_timeout0;
[*]    #keepalive_timeout65;
[*]    #gzipon;
[*]
[*]
[*]
[*]
[*]    server_names_hash_bucket_size 128;   #指定服务器名称哈希表的框大小
[*]
[*]
[*]    client_header_buffer_size 32k;         
[*]    large_client_header_buffers 4 128k;   #以上两个是设定客户端请求的Header头缓冲区大小,对于cookie内容较大的请求,应增大改值。(400或414错误)
[*]    client_max_body_size 8m;                #允许客户端请求的最大单文件字节数
[*]    client_body_buffer_size 32k;            #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
[*]   
[*]    proxy_connect_timeout 600;            #nginx跟后端服务器连接超时时间(代理连接超时)
[*]    proxy_read_timeout    600;            #连接成功后,后端服务器响应时间(代理接收超时)
[*]    proxy_send_timeout    600;            #后端服务器数据回传时间(代理发送超时)
[*]    proxy_buffer_size   32k;            #设置代理服务器(nginx)保存用户头信息的缓冲区大小
[*]    proxy_buffers         4 32k;            #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
[*]    proxy_busy_buffers_size64k;         #高负荷下缓冲大小(proxy_buffers*2)
[*]    proxy_temp_file_write_size1024m;      #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
[*]    proxy_ignore_client_abort on;         #不允许代理端主动关闭连接
[*]
[*]
[*]    sendfile         on;
[*]    tcp_nopush         on;
[*]    keepalive_timeout      65;
[*]    tcp_nodelay   on;
[*]    gzip         on;
[*]    gzip_min_length      1k;
[*]    gzip_buffers         4 16k;
[*]    gzip_http_version   1.0;
[*]    gzip_proxied         any;#前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
[*]    gzip_comp_level   2;
[*]    gzip_types      text/plain application/x-javascript text/css application/xml;
[*]    gzip_vary on;
[*]   
[*]
[*]
[*]    server_tokens off;
[*]
[*]
[*]    #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
[*]    proxy_temp_path   /cache/proxy_temp_path;
[*]    #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
[*]    proxy_cache_path/cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
[*]
[*]
[*]      
[*]    server {
[*]      listen       8012;
[*]      server_namelocalhost.net;
[*]
[*]
[*]      #charset koi8-r;
[*]
[*]
[*]      #access_loglogs/host.access.logmain;
[*]
[*]
[*]      location / {
[*]            root   /var/www/nginx;
[*]            indexindex.html index.htm;
[*]      }
[*]
[*]
[*]      #error_page404            /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;
[*]      }
[*]
[*]
[*]      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
[*]      #
[*]      #location ~ \.php$ {
[*]      #    proxy_pass   http://127.0.0.1;
[*]      #}
[*]
[*]
[*]      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
[*]      #
[*]      location ~ \.php$ {
[*]            proxy_passhttp://127.0.0.1:8011;
[*]            #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
[*]            #include      fastcgi_params;
[*]      }
[*]
[*]
[*]    #扩展名以.gif、.jpg、.css等结尾的静态文件缓存。
[*]      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|mp3|mp4|flv|f4v|wmv|wma|mov)$
[*]
[*]
[*]      {
[*]            #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
[*]      proxy_next_upstream http_502 http_504 error timeout invalid_header;
[*]
[*]
[*]      proxy_cache cache_one;                   #进行缓存,使用Web缓存区cache_one
[*]      proxy_cache_valid 200 304 12h;         #对不同的HTTP状态码设置不同的缓存时间
[*]      proxy_cache_valid 301 302 1m;
[*]      proxy_cache_valid any 1m;
[*]      proxy_cache_key $host$uri$is_args$args;#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
[*]      proxy_set_headerHost $host;
[*]      proxy_set_headerX-Real-IP$remote_addr;
[*]      proxy_set_header X-Forwarded-For $remote_addr;
[*]      proxy_set_header Accept-Encoding "none";#设定proxy_set_header Accept-Encoding '';   
[*]                                                #      (或是后台服务器关闭gzip),这样这台机器才
[*]                                                #       不会缓存被压缩的文件,造成乱码
[*]      #proxy_set_header Accept-Encoding ""; #这个也可
[*]      proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。
[*]      #proxy_pass http://127.0.0.1:8011; #反向代理
[*]      root   /var/www/nginx;      
[*]      expires1h;
[*]
[*]
[*]      }
[*]    #设置只允许指定的IP或IP段才可以清除URL缓存。
[*]      location ~ /purge(/.*){
[*]            allow            127.0.0.1;
[*]            allow            192.168.0.0/255;
[*]            allow             all;
[*]            proxy_cache_purge cache_one $host$1$is_args$args;
[*]      }   
[*]      # deny access to .htaccess files, if Apache's document root
[*]      # concurs with nginx's one
[*]      #
[*]      #location ~ /\.ht {
[*]      #    denyall;
[*]      #}
[*]    }
[*]    #测试不同域名绑定同一端口
[*]    server {
[*]      listen       8012;
[*]      server_namelocalhost.com;
[*]      location / {
[*]            root   /var/www/nginx2;
[*]            indexindex.html index.htm;
[*]      }
[*]      error_page   500 502 503 504/50x.html;
[*]      location = /50x.html {
[*]            root   html;
[*]      }
[*]      location ~ \.php$ {
[*]            proxy_passhttp://127.0.0.1:8011;
[*]      }
[*]    }
[*]      
[*]    # another virtual host using mix of IP-, name-, and port-based configuration
[*]    #
[*]    #server {
[*]    #    listen       8000;
[*]    #    listen       somename:8080;
[*]    #    server_namesomenamealiasanother.alias;
[*]
[*]
[*]    #    location / {
[*]    #      root   html;
[*]    #      indexindex.html index.htm;
[*]    #    }
[*]    #}
[*]
[*]
[*]
[*]
[*]    # HTTPS server
[*]    #
[*]    #server {
[*]    #    listen       443;
[*]    #    server_namelocalhost;
[*]
[*]
[*]    #    ssl                  on;
[*]    #    ssl_certificate      cert.pem;
[*]    #    ssl_certificate_keycert.key;
[*]
[*]
[*]    #    ssl_session_timeout5m;
[*]
[*]
[*]    #    ssl_protocolsSSLv2 SSLv3 TLSv1;
[*]    #    ssl_ciphersHIGH:!aNULL:!MD5;
[*]    #    ssl_prefer_server_ciphers   on;
[*]
[*]
[*]    #    location / {
[*]    #      root   html;
[*]    #      indexindex.html index.htm;
[*]    #    }
[*]    #}
[*]
[*]
[*]}



到此为止完成了cache配置,现在测试,在/var/www/nginx下放一个1.mq3文件,然后访问它:http://localhost.net/1.mp3,再看看/cache下是否cache了文件,有则接近成功啦。
:P
下次再研究nginx负载均衡的配置。
摘自:http://blog.csdn.net/esinzhong/article/details/6656066


ksftg 发表于 2014-2-11 07:35:37

继续关注一下这方面的信息











static/image/common/sigline.gif
深圳5V3A电源适配器
页: [1]
查看完整版本: nginx 反向代理做cache配置