nginx反向代理 proxy_cache缓存池

load_module modules/ngx_stream_module.so;   #动态加载模块,必须写道开头
user  nginx;   #使用useradd nginx    添加一个nginx用户
worker_processes  4;   #cpu核心数 * 2
worker_rlimit_nofile   102400;  #配置nginx打开最大文件数  (每个工作进程绑定一个cpu,worker_cpu_affinity配置)
worker_cpu_affinity 0001 0010 0100 1000;  #工作进程使用哪个cpu的核心 (以四核为例)  0001是4核的第一个核心 0010是4核的第二个核心 

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

pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  10240;
}


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;   #在server虚拟目录里面配置日志,这里是全局日志

    sendfile        on;   
    #tcp_nopush     on;

    

    server_tokens off;  #错误的时候关闭输出版本号


    #keepalive_timeout  0;
    keepalive_timeout  30;

	
	#gzip  数据压缩
	
    gzip  on;   #压缩会占用cpu
    gzip_buffers 4 16k;
    gzip_comp_level 3;  #压缩等级
    gzip_disable "MSIE[1-6]";   #ie浏览器1-6禁用gzip
    gzip_min_length 1k;
    gzip_http_version 1.0;
    gzip_types text/plaion application/html application/css application/js;  #可以压缩的文件类型
	
	#gzip end
	
	
    gzip_vary on;  #根据http头判断是否支持压缩

    client_max_body_size 8m;   #默认允许客户端最大上传文件大小
	
	
	include vhost/*.conf;  #加载所有vhost下的所有conf文件

	
	#proxy_cache 代理缓冲区配置
	
	proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=proxy_cache_zone:128m inactive=5m max_size=10g;
	proxy_buffering on;                     #默认on,是否缓存后端服务器响应
	proxy_buffer_size 64k;                  #默认缓存区大小
	proxy_buffers 8 32k;                    #指定多少与多大缓存区来缓存后端服务器响应
	proxy_temp_path proxy_temp 1 2;         #默认目录proxy_temp,1 2 是level ,意思是两级目录
	proxy_max_temp_file_size 1024m;         #默认1024m,单个临时文件最大大小
	proxy_temp_file_write_size 128k;        #默认16k,一次写入临时文件最大大小
	proxy_request_buffering on;             #默认on,是否先缓存整个客户端请求正文,在发送后端服务器
	proxy_ignore_headers Set-Cookie;         #忽略缓存cookie
	
	proxy_set_header Host $host;            #添加请求头Host字段值位本机IP地址
	proxy_set_header X-Real-IP $remote_addr;    #添加请求头X-Real-IP值位客户端IP
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #原始客户端IP 和 代理IP地址
	
	proxy_connect_timeout 60s;               #默认60s,与后端服务器建立连接超时时间
	proxy_read_timeout 300s;                 #默认60s,读取后端服务器响应超时时间
	proxy_send_timeout 300s;                 #默认60s,发送请求到后端服务器超时时间
	
	
	
	
	
	
	#proxy_cache_end
	
	
	
	
	
	
    server {
        listen       80;
        server_name  www.baidu.com www.baidu.cn;
        #server_name localhost;
        charset utf-8;

        access_log  logs/baidu.com_access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }


    }





}

这里是加载了  vhost/*.conf

server{
	listen 80;
	server_name localhost;
	access_log logs/test_access.log main;
	
	location / {
		 proxy_pass http://www.baidu.com;    #尾部不能有/
	}
	
	#这个需要放在前面,清理缓存
	location ~ /purge(.*){
		proxy_cache_purge proxy_cache_zone $host$1;
	}
	
	#缓存配置
	location ~\.(html|js|css|jpg|png){
		proxy_pass http://www.baidu.com;
		proxy_cache proxy_cache_zone;               #指定缓存区名称
		proxy_cache_key $host$request_uri;          #定义缓存的key,根据md5值为缓存文件名
		proxy_cache_valid 200 302 10m;              #为不同的状态码设置缓存时间 200,302 10分钟缓存
		proxy_cache_valid 301 1d;
		proxy_cache_valid any 1m;
		add_header X-Cache $upstream_cache_status;  #添加响应头,测试是否命中;代理服务器才有的变量
	}
	
	
}

清理缓存module安装步骤


如果需要重新添加编译参数

./nginx -V 查看编译参数
复制编译参数 --add-module=/root/ngx_cache_purge/


cd ~   #cd到root根目录  
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz


cd /opt/work/nginx   #cd到nginx源码目录


./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --with-stream=dynamic --with-http_stub_status_module --add-module=/root/ngx_cache_purge-2.3


然后  
make


make完成后,把编译好的文件复制到/usr/local/nginx/sbin/nginx目录里面


pkill nginx   #关闭nginx进程

cp       objs/nginx       /usr/local/nginx/sbin/nginx


缓存的清理 ,如缓存的资源地址为

http://www.baidu.com/logo.jpg

http://www.baidu.com/purge/logo.jpg  这样就把logo.jpg这个缓存清理了


猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/80737529