星哥的博客

  • 首页
  • 系统
  • 虚拟机
  • 网站开发
  • 硬件外设
  • VPS/云主机
  • 心情随笔
  1. 首页
  2. 网站开发
  3. Nginx
  4. 正文

Nginx缓存服务器 替代Squid

2012年1月15日 2172点热度 0人点赞 0条评论

最近Squid不知为何是很不稳定,查看张宴老师的博客,看到Nginx的缓存效果也不错,就动手试试。

发现Nginx一个大问题,不能对比当前缓存的Last-Modified,如果后端服务器更新文件,Nginx仍会把缓存扔给用户,而不是去更新缓存,直到缓存过期才去更新,squid对去对比,然后更新。不知道有没有办法解决,网上貌似没有对此的讨论,奇怪。

以下为更新为最新版本的安装代码:

ulimit -SHn 65535

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar zxvf pcre-8.21.tar.gz
cd pcre-8.21/
./configure
make && make install
cd ../

wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz
tar zxvf ngx_cache_purge-1.5.tar.gz

wget http://nginx.org/download/nginx-1.0.11.tar.gz
tar zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.5 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

/usr/local/webserver/nginx/sbin/nginx

nginx.conf配置如下:

user  www www;

worker_processes 8;

worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
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; //建议设置成256k或512k

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和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path   /home/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path  /home/proxy_cache_dir  levels=1:2   keys_zone=cache_one:300m inactive=3d max_size=9g;

server
{
listen       80;
server_name  www.123.cn;
root  /home/wwwroot;

location ~ .*\.(jpg|cod|zip|gif|png|js|css)?$
{
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid  200 304 24h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers "Cache-Control" "Expires";
proxy_pass http://www.123.cn:80;
expires      3d;
}

location /
{
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers "Cache-Control" "Expires";
proxy_pass http://www.123.cn:80;
}

location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}

#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow  all;
proxy_cache_purge    cache_one   $host$1$is_args$args;
}

log_format  www.123.cn  '$remote_addr - $remote_user [$time_local] $request '
'$status $body_bytes_sent $http_referer '
'$http_user_agent $http_x_forwarded_for'
'$upstream_cache_status $upstream_status';
access_log  /home/wwwlogs/www.123.cn.log  www.123.cn;
}
}

这里对nginx.conf做了简单的修改,因为不需要均衡负载,所以直接转发到相应的服务器上,而且这里我是做为转发代理服务器使用,所以只需要缓存固定的几种文件,其他都直接转发过去,不进行处理。

不知道为什么purge老是无法使用,很奇怪,在研究研究。

标签: 暂无
最后更新:2012年1月15日

wangxingcs

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据。

wangxingcs

这个人很懒,什么都没留下

我的博客

仅记录一些有用的教程,自己的经历,已屏蔽各大搜索引擎收录。

新浪微博 @wangxingcs

Twitter @wangxingcs(常在)

最新 热点 随机
最新 热点 随机
Proxmox VE(PVE)6.X 添加硬盘 PVE解决VM is locked问题 Proxmox VE配置更新源 黑群晖显示真实CPU信息(支持7.0.1) Proxmox VE直通硬盘(全盘映射方式) PVE去除“没有有效订阅”提示
Proxmox VE(PVE)6.X 添加硬盘
目前兼容性最好的CSS3圆角写法 CentOS下安装PPTP VPN小记 织梦独立模型的动态浏览修改方法 Proxmox VE配置更新源 纯CSS实现侧边栏/分栏高度自动相等 【转】IIS7和7.5解析漏洞解决方法
标签聚合
DSM 群晖 虚拟机 MTP FineCMS CMS 驱动 PVE

COPYRIGHT © 2015-2023 wangxingcs.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备13007295号