Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1068057
  • 博文数量: 83
  • 博客积分: 159
  • 博客等级: 上尉
  • 技术积分: 2221
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 17:08
个人简介

……致我那曾经苦逼的岁月……

文章分类
文章存档

2018年(1)

2017年(7)

2016年(13)

2014年(1)

2013年(12)

2012年(27)

2011年(22)

分类: 系统运维

2013-12-27 21:32:45

今天试了一下nginx缓存配置,说到缓存相信大家应该都不陌生,也就是把访问后端web服务器的静态请求缓存到本地文件系统一个目录,后端一般是apache服务器居多!当前这两台机器完全可以在一台服务器上,只是开启不同的端口运行即可!下面我就写一个最简单配置的缓存服务器实验吧!
需求:将访问后端apache服务器的静态请求缓存到一个目录下。然后关闭后端的apache服务器,看看是否还能访问之前静态网页!
环境:CentOS6.4(64位)
1、安装nginx和apache
yum install nginx httpd -y
2、配置nginx,配置文件如下:

user nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
worker_rlimit_nofile 65535;

error_log   /var/log/nginx/error.log info;

pid    /var/run/nginx.pid;
events {
  worker_connections  65535;
}

http {
include       mime.types;
default_type  application/octet-stream;
log_format  access  ' $remote_addr - $remote_user [$time_local] "$request" '
                             '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" $http_x_forwarded_for';

sendfile on;
keepalive_timeout 0;

##缓存配置
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /var/nginx/temp_dir;           设置临时目录
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;          设置缓存目录,和上面的目录必须在一个分区里面
gzip    on;
upstream appserver {
          server 192.168.0.1:8080; 
}
server {
          listen       80 default;
          server_name 192.168.0.1;
location /
{             
          proxy_pass ;
          proxy_cache cache_one;   
          proxy_cache_methods GET HEAD POST;   
          proxy_cache_min_uses 1;   
          proxy_cache_valid 200 302 10m;       200和302状态缓存10分钟,10分钟后失效   
          proxy_cache_valid 404 1m;               404状态缓存1分钟
          proxy_cache_valid any 1m;                其余状态缓存1分钟
          access_log /var/log/nginx/192.168.0.1.log;
          }
     }
}

3、检查nginx配置文件并启动nginx
/etc/init.d/nginx configtest
如果出错应该是/var/nginx目录需要创建。
mkdir /var/nginx
/etc/init.d/nginx start
4、将/etc/httpd/conf/httpd.conf配置文件里面的端口修改为8080
5、写一个静态测试页面
echo "hello" > /var/www/html/index.html
6、测试
访问是否出现hello字样。    ------>正常会出现
查看/var/nginx/cache目录下是否有目录,目录中是否有文件?记录下文件的时间戳      -------有目录,目下有文件
关闭apache,继续刷新网页,是否能正常访问?      -------->可以继续访问
对着文件的时间戳,过10分钟再刷新是否出现502 Bad Gateway页面        -------->是
说明成功!
阅读(8726) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~