您的位置:首页 > 运维架构 > 反向代理

nginx 反向代理缓存服务器配置

2015-07-19 03:07 621 查看
修改proxy.conf,配置http全局环境
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 64k;           #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户proxy_connect_timeout 600;             #nginx跟后端服务器连接超时时间(代理连接超时)proxy_read_timeout    600;             #连接成功后,后端服务器响应时间(代理接收超时)proxy_send_timeout    600;             #后端服务器数据回传时间(代理发送超时)proxy_buffer_size     64k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小proxy_buffers         4 64k;           #proxy_buffers缓冲区,网页平均在64k以下的话,这样设置proxy_busy_buffers_size  128k;         #高负荷下缓冲大小(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	2k;                    #最小压缩文件大小gzip_buffers	 4 16k;gzip_http_version 1.0;gzip_proxied	 any;                  #前端是squid的情况下要加此参数,否则squid上不缓存gzip文件gzip_comp_level 5;gzip_types	   text/plain  text/css  text/htm  application/xml application/x-javascript;gzip_vary      on;server_tokens  off;#设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为5GB。proxy_temp_path   /tmp/proxy_temp_path;proxy_cache_path  /tmp/proxy_cache_path levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=5g;
创建缓存存放目录
mkdir /tmp/proxy_temp_pathmkdir /tmp/proxy_cache_pathchmod -R 777 /tmp/proxy_*echo "mkdir -p /tmp/proxy_temp_path" >> /etc/rc.localecho "mkdir -p /tmp/proxy_cache_path" >> /etc/rc.localecho "chmod -R 777 /tmp/proxy_*" >> /etc/rc.local
修改nginx.cnf,配置nginx做前端代理缓存服务器
http {include       proxy.conf;
...
upstream  tomcat_groups {ip_hash;
server 192.168.0.1:8080;
server 192.168.0.2:8080;}server {listen 80 default;
server_name _;return 500;
access_log off;}server {listen 80;
server_name  testA.domian.com testB.domian.com  testC.domian.com;

location / {proxy_next_upstream http_502 http_504 error timeout invalid_header; #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。proxy_cache cache_one; #进行缓存,使用Web缓存区cache_oneproxy_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 $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Accept-Encoding "none"; #避免缓存被压缩的文件造成乱码proxy_ignore_headers "Cache-Control" "Expires"; #proxy_cache支持后台设定的expires。proxy_pass http://tomcat_groups; expires 12h;}location ~ .*\.(php|jsp|aspx|cgi|xml)?$  {proxy_set_header  Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_groups;}location ~ /purge(/.*)  {allow    127.0.0.1;
allow    192.168.0.0/24; #设置只允许指定的IP或IP段才可以清除URL缓存。allow    all;
proxy_cache_purge cache_one $host$1$is_args$args;}   }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: