找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4568|回复: 0

Nginx+proxy_cache高速缓存配置

[复制链接]
发表于 2012-2-7 10:31:35 | 显示全部楼层 |阅读模式
前言* Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。
  1. 一、 安装nginx和ngx-purge:
  2. ulimit -SHn 65535
  3. yum install pcre pcre-devel -y 安装pcrewget http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
  4. tar zxvf ngx_cache_purge-1.4.tar.gz
  5. wget http://nginx.org/download/nginx-1.0.11.tar.gz
  6. tar zxvf nginx-1.0.11.tar.gz
  7. cd nginx-1.0.11/
  8. ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  9. make && make install
  10. cd ../二、 配置nginx.conf文件如下配置文件:
  11. user www www;worker_processes 8;error_log /data/logs/nginx/error.log crit;pid /usr/local/nginx/nginx.pid;#Specifies the value for maximum file descriptors that can be opened by this process.
  12. worker_rlimit_nofile 65535;events
  13. {
  14. use epoll;
  15. worker_connections 65535;
  16. }http
  17. {
  18. include mime.types;
  19. default_type application/octet-stream;charset utf-8;server_names_hash_bucket_size 128;
  20. client_header_buffer_size 32k;
  21. large_client_header_buffers 4 32k;
  22. client_max_body_size 300m;sendfile on;
  23. tcp_nopush on;keepalive_timeout 60;tcp_nodelay on;client_body_buffer_size 512k;
  24. proxy_connect_timeout 5;
  25. proxy_read_timeout 60;
  26. proxy_send_timeout 5;
  27. proxy_buffer_size 16k;
  28. proxy_buffers 4 64k;
  29. proxy_busy_buffers_size 128k;
  30. proxy_temp_file_write_size 128k;gzip on;
  31. gzip_min_length 1k;
  32. gzip_buffers 4 16k;
  33. gzip_http_version 1.1;
  34. gzip_comp_level 2;
  35. gzip_types text/plain application/x-javascript text/css application/xml;
  36. gzip_vary on;proxy_temp_path /data/proxy_temp_dir;
  37. #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  38. proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;upstream backend_server {
  39. server 192.168.5.130:8080 weight=1 max_fails=2 fail_timeout=30s;
  40. server 192.168.5.131:8080 weight=1 max_fails=2 fail_timeout=30s;
  41. }server
  42. {
  43. listen 80;
  44. server_name www.abc.com 192.168.5.133;
  45. index index.html index.htm;
  46. root /data/webapps/www;location /
  47. {
  48. #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
  49. proxy_next_upstream http_502 http_504 error timeout invalid_header;
  50. proxy_cache cache_one;
  51. #对不同的HTTP状态码设置不同的缓存时间
  52. proxy_cache_valid 200 304 12h;
  53. #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
  54. proxy_cache_key $host$uri$is_args$args;
  55. proxy_set_header Host $host;
  56. proxy_set_header X-Forwarded-For $remote_addr;
  57. proxy_pass http://backend_server;
  58. expires 1d;
  59. }location ~ /purge(/.*)
  60. {
  61. #设置只允许指定的IP或IP段输入正确的密码才可以清除URL缓存。
  62. auth_basic “Please Insert User And Password”;
  63. auth_basic_user_file /usr/local/nginx/conf/htpasswd;
  64. allow 127.0.0.1;
  65. allow 192.168.1.0/24;
  66. deny all;
  67. proxy_cache_purge cache_one $host$1$is_args$args;
  68. }location ~ .*\.(php|jsp|cgi)?$
  69. {
  70. proxy_set_header Host $host;
  71. proxy_set_header X-Forwarded-For $remote_addr;
  72. proxy_pass http://backend_server;
  73. }
  74. log_format access ‘$remote_addr – $remote_user[$time_local] “$request” ‘
  75. ‘$status $body_bytes_sent “$http_referer” ‘
  76. ‘”$http_user_agent” $http_x_forwarded_for’;
  77. access_log /data/logs/nginx/access.log access;
  78. }
  79. }
复制代码
三、 启动nginx即可:
/usr/local/nginx/sbin/nginx 即可
然后配置好resin端口设置为8080
如果需要刷新缓存的url地址为:
如下图:
本文出自 “【烟雨楼台】” 博客,请务必保留此出处http://wgkgood.blog.51cto.com/1192594/773278

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-30 08:08 , Processed in 0.011737 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表