nginx 反向代理做cache配置

6 篇文章 0 订阅
2 篇文章 0 订阅

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

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文件

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

#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负载均衡的配置。




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx是一个高性能的开源Web服务器反向代理服务器。它可以通过反向代理来将客户端的请求转发给后端的服务器,并将响应返回给客户端。下面是nginx反向代理配置规则的介绍: 1. 配置反向代理服务器: 在nginx配置文件中,使用`server`块来定义反向代理服务器配置。例如: ``` server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; } } ``` 上述配置中,`listen`指定了监听的端口,`server_name`指定了域名,`location`指定了匹配的URL路径,`proxy_pass`指定了后端服务器的地址。 2. 配置负载均衡: nginx可以通过负载均衡来分发请求到多个后端服务器,以提高系统的性能和可靠性。常见的负载均衡算法有轮询、IP哈希、最少连接等。例如: ``` upstream backend_servers { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend_servers; proxy_set_header Host $host; } } ``` 上述配置中,`upstream`定义了后端服务器的列表,`proxy_pass`指定了后端服务器的地址,`proxy_set_header`用于设置请求头。 3. 配置缓存: nginx可以通过缓存来提高响应速度和减轻后端服务器的负载。可以使用`proxy_cache`和`proxy_cache_key`指令来配置缓存。例如: ``` http { proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m; server { listen 80; server_name example.com; location / { proxy_pass http://backend_server; proxy_cache my_cache; proxy_cache_key $host$uri$is_args$args; } } } ``` 上述配置中,`proxy_cache_path`指定了缓存路径和大小,`proxy_cache`指定了使用的缓存区域,`proxy_cache_key`用于设置缓存的键。 4. 配置SSL/TLS: nginx可以通过配置SSL/TLS来实现HTTPS的支持。可以使用`ssl_certificate`和`ssl_certificate_key`指令来配置证书和私钥。例如: ``` server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; location / { proxy_pass http://backend_server; proxy_set_header Host $host; } } ``` 上述配置中,`listen`指定了监听的端口和使用SSL,`ssl_certificate`和`ssl_certificate_key`指定了证书和私钥的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值