Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120317
  • 博文数量: 95
  • 博客积分: 316
  • 博客等级: 二等列兵
  • 技术积分: 635
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-23 17:41
文章分类

全部博文(95)

文章存档

2014年(1)

2013年(1)

2012年(93)

我的朋友

分类: 网络与安全

2012-02-02 21:38:36

 1、Nginx 负载均衡与缓存服务器在 Linux 下的编译安装:
  1. ulimit -SHn 65535   
  2. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz   
  3. tar zxvf pcre-8.00.tar.gz   
  4. cd pcre-8.13/   
  5. ./configure   
  6. make && make install   
  7. cd ../   
  8.  
  9. wget -1.0.tar.gz   
  10. tar zxvf ngx_cache_purge-1.0.tar.gz   
  11.  
  12. wget -0.8.32.tar.gz   
  13. tar zxvf nginx-0.8.32.tar.gz   
  14. cd nginx-0.8.32/   
  15. ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module   
  16. make && make install   
  17. cd ../   

2、/usr/local/webserver/nginx/conf/nginx.conf 配置文件内容如下:

  1. user www www;   
  2.  
  3. worker_processes 8;   
  4.  
  5. error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;   
  6.  
  7. pid /usr/local/webserver/nginx/nginx.pid;   
  8.  
  9. #Specifies the value for maximum file descriptors that can be opened by this process.   
  10. worker_rlimit_nofile 65535;   
  11.  
  12. events   
  13. {   
  14. use epoll;   
  15. worker_connections 65535;   
  16. }   
  17.  
  18. http   
  19. {   
  20. include mime.types;   
  21. default_type application/octet-stream;   
  22.  
  23. charset utf-8;   
  24.  
  25. server_names_hash_bucket_size 128;   
  26. client_header_buffer_size 32k;   
  27. large_client_header_buffers 4 32k;   
  28. client_max_body_size 300m;   
  29.  
  30. sendfile on;   
  31. tcp_nopush on;   
  32.  
  33. keepalive_timeout 60;   
  34.  
  35. tcp_nodelay on;   
  36.  
  37. client_body_buffer_size 512k;   
  38. proxy_connect_timeout 5;   
  39. proxy_read_timeout 60;   
  40. proxy_send_timeout 5;   
  41. proxy_buffer_size 16k;   
  42. proxy_buffers 4 64k;   
  43. proxy_busy_buffers_size 128k;   
  44. proxy_temp_file_write_size 128k;   
  45.  
  46. gzip on;   
  47. gzip_min_length 1k;   
  48. gzip_buffers 4 16k;   
  49. gzip_http_version 1.1;   
  50. gzip_comp_level 2;   
  51. gzip_types text/plain application/x-javascript text/css application/xml;   
  52. gzip_vary on;   
  53.  
  54. #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区   
  55. proxy_temp_path /data0/proxy_temp_dir;   
  56. #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天清理一次缓存,硬盘缓存空间大小为30GB。   
  57. proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;   
  58.  
  59. upstream backend_server {   
  60. server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;   
  61. server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;   
  62. server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;   
  63. }   
  64.  
  65. server   
  66. {   
  67. listen 80;   
  68. server_name   
  69. index index.html index.htm;   
  70. root /data0/htdocs/www;   
  71.  
  72. location /   
  73. {   
  74. #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。   
  75. proxy_next_upstream http_502 http_504 error timeout invalid_header;   
  76. proxy_cache cache_one;   
  77. #对不同的HTTP状态码设置不同的缓存时间   
  78. proxy_cache_valid 200 304 12h;   
  79. #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内   
  80. proxy_cache_key $host$uri$is_args$args;   
  81. proxy_set_header Host $host;   
  82. proxy_set_header X-Forwarded-For $remote_addr;   
  83. proxy_pass   
  84. expires 1d;   
  85. }   
  86.  
  87. #用于清除缓存,假设一个URL为,通过访问就可以清除该URL的缓存。   
  88. location ~ /purge(/.*)   
  89. {   
  90. #设置只允许指定的IP或IP段才可以清除URL缓存。   
  91. allow 127.0.0.1;   
  92. allow 192.168.0.0/16;   
  93. deny all;   
  94. proxy_cache_purge cache_one $host$1$is_args$args;   
  95. }   
  96.  
  97. #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。   
  98. location ~ .*\.(php|jsp|cgi)?$   
  99. {   
  100. proxy_set_header Host $host;   
  101. proxy_set_header X-Forwarded-For $remote_addr;   
  102. proxy_pass   
  103. }   
  104.  
  105. access_log off;   
  106. }   
  107. }   
  108.    

3、启动 Nginx:

  1. /usr/local/webserver/nginx/sbin/nginx   
  2.  

4、清除指定的URL缓存示例:

原文地址:http://blog.s135.com/nginx_cache/



阅读(1017) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Ryanliang_cu2012-02-02 22:02:20

[root@dev sbin]# ./nginx
nginx: [emerg] getpwnam("www") failed in /usr/local/webserver/nginx/conf/nginx.conf:3