一. 东西不多,直接拷贝脚本跑完就是成功的安装


ulimit -SHn 65535 
groupadd -g 80 www
useradd -g www -u 80 www
yum install pcre pcre-devel rsync gcc -y
mkdir /opt/soft
cd /opt/soft
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar -xf ngx_cache_purge-2.1.tar.gz
wget  http://nginx.org/download/nginx-1.4.0.tar.gz
tar -xf nginx-1.4.0.tar.gz
cd nginx-1.4.0
./configure --user=www --group=www --add-module=../ngx_cache_purge-2.1 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
mkdir -p /data/nginx/logs
mkdir  -p /data/nginx/proxy_cache_dir
mkdir  -p /data/nginx/proxy_cache_tmp
                                                                                                                
chown www.www /data/nginx -R
/usr/local/nginx/sbin/nginx -t  && /usr/local/nginx/sbin/nginx


二. 配置nginx.conf 实现缓存代理,直接拷贝替换,到这里已经完成配置

vim   /usr/local/nginx/conf/nginx.conf
user www www; 
                                                                          
worker_processes 8; 
                                                                          
error_log /data/nginx/logs/error.log crit; 
                                                                          
pid /usr/local/nginx/nginx.pid; 
                                                                          
worker_rlimit_nofile 65535; 
                                                                          
events 
{ 
        use epoll; 
        worker_connections 65535; 
} 
                                                                          
http 
{ 
        include mime.types; 
        default_type application/octet-stream; 
                                                                          
        charset utf-8; 
                                                                                  
        server_names_hash_bucket_size 128; 
        client_header_buffer_size 32k; 
        large_client_header_buffers 4 32k; 
        client_max_body_size 300m; 
                                                                                  
        sendfile on; 
        tcp_nopush on; 
                                                                                  
        keepalive_timeout 60; 
                                                                                  
        tcp_nodelay on; 
                                                                                  
        client_body_buffer_size 512k; 
        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; 
                                                                                  
        gzip on; 
        gzip_min_length 1k; 
        gzip_buffers 4 16k; 
        gzip_http_version 1.1; 
        gzip_comp_level 2; 
        gzip_types text/plain application/x-javascript text/css application/xml; 
        gzip_vary on; 
                                                                                  
        proxy_temp_path /data/nginx/proxy_cache_tmp; 
        proxy_cache_path /data/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:6000m inactive=1d max_size=400g; 
                                                                                  
        #include vhosts/* ;
        log_format cache '$request_time - $upstream_cache_status ' '$status $remote_addr - $remote_user [$time_local] - $request' '$body_bytes_sent $http_referer' 'upstream_response_time $upstream_response_time ' 'msec $msec';
        access_log /data/nginx/logs/access.log cache;
        upstream cdnups {
                server www.baidu.com:80 weight=100 max_fails=3 fail_timeout=30s;
        }
                                                                                 
        server 
        { 
                listen 80; 
                server_name www.baidu.com; 
                index index.html index.htm; 
                                                                                         
                                                                                          
                location /
                { 
                        #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。 
                        proxy_next_upstream http_502 http_504 error timeout invalid_header; 
                        proxy_cache cache_one; 
                        #对不同的HTTP状态码设置不同的缓存时间 
                        proxy_cache_valid 200 304 12h; 
                        #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 
                        proxy_cache_key $host$uri$is_args$args; 
                        proxy_set_header Host $host; 
                        proxy_set_header X-Forwarded-For $remote_addr; 
                        proxy_pass http://cdnups; 
                        #expires 30d; 
                } 
                                                                                  
                location ~ /purge(/.*)  
                { 
                                                                                                 
                        allow 127.0.0.1; 
                        allow 192.168.1.1;
                        deny all; 
                        proxy_cache_purge cache_one $host$1$is_args$args; 
                } 
                                                                                          
                location ~ .*\.(php|jsp|cgi|do|shtml)?$
                { 
                        proxy_set_header Host $host; 
                        proxy_set_header X-Forwarded-For $remote_addr; 
                        proxy_pass http://cdnups; 
                }
        }
                                                                                
}


三. 重启 nginx

kill -HUP `cat /usr/local/nginx/nginx.pid`


四. 测试 nginx 代理,假设搭建的机器是192.168.100.100

#绑定hosts
192.168.100.100  www.baidu.com
#通过浏览器访问 www.baiud.com
#如果日志文件  /data/nginx/logs/access.log 有记录,说明搭建成功