六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 492|回复: 1

nginx 反向代理做cache配置

[复制链接]
 楼主| 发表于 2013-12-11 19:22:06 | 显示全部楼层 |阅读模式
nginx 的cache功能需要第三方模块的支持:ngx_cache_purge
下载地址:http://labs.frickle.comhttps://github.com/FRiCKLE/ngx_cache_purge/

[plain] 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文件[plain] view plaincopy

  • cd /usr/local/nginx/conf  
  • sudo gedit nginx.conf  


[plain] view plaincopy

  • #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;  
  •   
  •   
  •     #sendfile        on;  
  •     #tcp_nopush     on;  
  •   
  •   
  •     #keepalive_timeout  0;  
  •     #keepalive_timeout  65;  
  •     #gzip  on;  
  •   
  •   
  •   
  •   
  •     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_size  64k;           #高负荷下缓冲大小(proxy_buffers*2)  
  •     proxy_temp_file_write_size  1024m;      #设定缓存文件夹大小,大于这个值,将从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_name  localhost.net;  
  •   
  •   
  •         #charset koi8-r;  
  •   
  •   
  •         #access_log  logs/host.access.log  main;  
  •   
  •   
  •         location / {  
  •             root   /var/www/nginx;  
  •             index  index.html index.htm;  
  •         }  
  •   
  •   
  •         #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;  
  •         }  
  •   
  •   
  •         # 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_pass  http://127.0.0.1:8011;  
  •             #fastcgi_param  SCRIPT_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_header  Host $host;  
  •         proxy_set_header  X-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;      
  •         expires  1h;  
  •   
  •   
  •         }  
  •     #设置只允许指定的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 {  
  •         #    deny  all;  
  •         #}  
  •     }  
  •     #测试不同域名绑定同一端口  
  •     server {  
  •         listen       8012;  
  •         server_name  localhost.com;  
  •         location / {  
  •             root   /var/www/nginx2;  
  •             index  index.html index.htm;  
  •         }  
  •         error_page   500 502 503 504  /50x.html;  
  •         location = /50x.html {  
  •             root   html;  
  •         }  
  •         location ~ \.php$ {  
  •             proxy_pass  http://127.0.0.1:8011;  
  •         }  
  •     }  
  •       
  •     # another virtual host using mix of IP-, name-, and port-based configuration  
  •     #  
  •     #server {  
  •     #    listen       8000;  
  •     #    listen       somename:8080;  
  •     #    server_name  somename  alias  another.alias;  
  •   
  •   
  •     #    location / {  
  •     #        root   html;  
  •     #        index  index.html index.htm;  
  •     #    }  
  •     #}  
  •   
  •   
  •   
  •   
  •     # HTTPS server  
  •     #  
  •     #server {  
  •     #    listen       443;  
  •     #    server_name  localhost;  
  •   
  •   
  •     #    ssl                  on;  
  •     #    ssl_certificate      cert.pem;  
  •     #    ssl_certificate_key  cert.key;  
  •   
  •   
  •     #    ssl_session_timeout  5m;  
  •   
  •   
  •     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  •     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  •     #    ssl_prefer_server_ciphers   on;  
  •   
  •   
  •     #    location / {  
  •     #        root   html;  
  •     #        index  index.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


该会员没有填写今日想说内容.

升级  79.33%

0

主题

0

主题

0

主题

举人

Rank: 3Rank: 3

积分
438
发表于 2014-2-11 07:35:37 | 显示全部楼层
继续关注一下这方面的信息












深圳5V3A电源适配器
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表