且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

squid反向代理网站配置示例

更新时间:2022-09-22 08:30:42

squid编译安装参数 


  1. ./configure --prefix=/usr/local/squid \ 
  2. --disable-carp \ 
  3. --with-aufs-threads=32 \ 
  4. --with-pthreads \ 
  5. --with-large-files \ 
  6. --enable-storeio='ufs,aufs' \ 
  7. --enable-disk-io='AIO,Blocking' \ 
  8. --enable-removal-policies='heap,lru' \ 
  9. --disable-wccp \ 
  10. --enable-kill-parent-hack \ 
  11. --enable-snmp \ 
  12. --disable-poll \ 
  13. --disable-select \ 
  14. --disable-ipv6   \ 
  15. --enable-auth=basic \ 
  16. --with-aio \ 
  17. --disable-ident-lookup \ 
  18. --enable-useragent-log \ 
  19. --enable-referer-log   \ 
  20. --with-filedescriptors=65536 \ 
  21. --with-pidfile=/var/run/squid.pid 

如果出现下面的错误


  1. configure: error: C++ compiler cannot create executables 
  2. See `config.log' for more details 
  3. configure: error: ./configure failed for lib/libTrie 

请安装


  1. yum install kdevelop 
  2. yum install gcc-c++ 

安装


  1. #make && make install 

初始化cache目录


  1. # squid -z 

squid.conf配置


  1. http_port 80 accel vhost vport 
  2.  
  3. logfile_rotate 10 
  4. log_fqdn off 
  5.  
  6. cache_swap_low 90 
  7. cache_swap_high 97 
  8. cache_mem 10240 MB 
  9. cache_mgr Henry 
  10. cachemgr_passwd password all 
  11.  
  12. cache_effective_user squid 
  13. cache_effective_group squid 
  14.  
  15. coredump_dir /usr/local/squid/var/cache 
  16. cache_dir ufs /usr/local/squid/var/cache 51200 16 256 
  17.  
  18. cache_replacement_policy lru 
  19. memory_replacement_policy lru 
  20.  
  21. cache_store_log none 
  22. cache_access_log /usr/local/squid/var/logs/access.log 
  23. cache_log /usr/local/squid/var/logs/cache.log 
  24.  
  25. fqdncache_size 1024 
  26. mime_table /usr/local/squid/etc/mime.conf 
  27. error_directory /usr/local/squid/share/errors/en-us/ 
  28. icon_directory  /usr/local/squid/share/icons/ 
  29. err_page_stylesheet /usr/local/squid/etc/errorpage.css 
  30. emulate_httpd_log on 
  31. max_open_disk_fds 0 
  32.  
  33. maximum_object_size 50 MB 
  34. minimum_object_size 0 KB 
  35. maximum_object_size_in_memory 5120 KB 
  36.  
  37. pipeline_prefetch on 
  38. forwarded_for off 
  39. forward_timeout 2 minutes 
  40. client_lifetime 1 hours 
  41. client_persistent_connections off 
  42. server_persistent_connections on 
  43. half_closed_clients off 
  44. httpd_suppress_version_string on 
  45. check_hostnames off 
  46.  
  47. visible_hostname proxy.xxx.xxx
  48. pid_filename /var/run/squid.pid 
  49. vary_ignore_expire off 
  50. negative_ttl 0 minutes 
  51.  
  52. reply_header_access Server deny all 
  53. reply_header_access X-Cache deny all 
  54. reply_header_access Warning deny all 
  55. reply_header_access Expires deny all 
  56. reply_header_access Cache-Control deny all 
  57. reply_header_access age deny all 
  58.  
  59. acl localhost src 127.0.0.1/32 
  60. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 
  61. acl manager proto cache_object 
  62.  
  63. acl SSL_ports port 443 563 
  64. acl Safe_ports port 80 # http 
  65. acl Safe_ports port 3130 # icp 
  66. acl Safe_ports port 21 # ftp 
  67. acl Safe_ports port 443 # https 
  68. acl Safe_ports port 70 # gopher 
  69. acl Safe_ports port 210 # wais 
  70. acl Safe_ports port 1025-65535 # unregistered ports 
  71. acl Safe_ports port 280 # http-mgmt 
  72. acl Safe_ports port 488 # gss-http 
  73. acl Safe_ports port 591 # filemaker 
  74. acl Safe_ports port 777 # multiling http 
  75. acl CONNECT method CONNECT 
  76. acl PURGE method PURGE 
  77.  
  78. icp_port 3130 
  79. icp_hit_stale off 
  80. icp_access allow all 
  81.  
  82. cache_peer 127.0.0.1 parent 8080 0 no-query no-digest no-netdb-exchange originserver name=a
  83. cache_peer 127.0.0.1 parent 8080 0 no-query no-digest no-netdb-exchange originserver name=b
  84. cache_peer 127.0.0.1 parent 8080 0 no-query no-digest no-netdb-exchange originserver name=c
  85.  
  86. #cache_peer 192.168.1.160 sibling 3128 3130 name=cache01 
  87. #cache_peer 192.168.1.130 sibling 80 3130 name=cache02 
  88. #cache_peer 192.168.1.159 sibling 3128 3130 name=cache03 
  89.  
  90. cache_peer_domain a xxx.xxx.xxx.xxx
  91. cache_peer_domain b xxx.xxx.xxx.xxx 
  92. cache_peer_domain c xxx.xxx.xxx.xxx
  93.  
  94. cache_peer_access a allow all 
  95. cache_peer_access b allow all 
  96. cache_peer_access c allow all 
  97.  
  98. peer_connect_timeout 30 seconds 
  99. request_timeout 1 minutes 
  100. persistent_request_timeout 2 minutes 
  101.  
  102. hierarchy_stoplist -i ^https:\\ ? cgi-bin ? 
  103. acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi Servlet 
  104. acl denyssl urlpath_regex -i ^https:\\ 
  105. no_cache deny QUERY 
  106. no_cache deny denyssl 
  107. #acl broken302 http_status 400-404 302 
  108.  
  109. http_access allow manager localhost 
  110. http_access allow PURGE localhost 
  111. http_access deny !Safe_ports 
  112. http_access deny CONNECT !SSL_ports 
  113. #http_access deny broken302 
  114. http_access allow localhost all 
  115. http_access allow all 
  116. http_access deny all 
  117.  
  118. #SNMP with Cacti config here 
  119. snmp_port 3401 
  120. acl snmppublic snmp_community valesquid 
  121. snmp_access allow snmppublic localhost 
  122. snmp_access deny all 
  123. #snmp_incoming_address 0.0.0.0 
  124. #snmp_outgoing_address 0.0.0.0 
  125.  
  126. refresh_pattern ^ftp:           1440    20%     10080 
  127. refresh_pattern ^gopher:        1440    0%      1440 
  128. refresh_pattern -i (/cgi-bin/|\?) 0     0%      0 
  129. refresh_pattern .               0       20%     4320 
  130. refresh_pattern -i \.jpg$       1440    90%     2880 
  131. refresh_pattern -i \.gif$       1440    90%     2880 
  132. refresh_pattern -i \.jpg$       1440    50%     2880 
  133. refresh_pattern -i \.rar$       1440    50%     2880 
  134. refresh_pattern -i \.zip$       1440    50%     2880 
  135. refresh_pattern -i \.doc$       1440    50%     2880 
  136. refresh_pattern -i \.ppt$       1440    50%     2880 
  137. refresh_pattern -i \.js$        60      50%     2880 
  138. refresh_pattern -i \.css$       60      50%     2880 
  139. refresh_pattern -i \.html$      60      50%     1440 
  140. refresh_pattern -i \.txt$       1440    50%     2880 

后端的apache虚拟主机部分


  1. # ----------------------------- 
  2. # add by Henry He on 2011/04/11 
  3. # ----------------------------- 
  4. <VirtualHost *:8080> 
  5.     ServerAdmin  xxx.xxx.xxx
  6.     ServerName   xxx.xxx.xxx
  7.     ServerAlias  xxx.xxx.xxx 
  8.     DocumentRoot "/var/www/xxxx" 
  9.     #ErrorLog     "logs/xxx.xxx.xxx-error_log" 
  10.     #CustomLog    "logs/xxx.xxx.xxx-access_log" combined 
  11. </VirtualHost> 
  12.  
  13. <VirtualHost *:8080> 
  14.     ServerAdmin  xxx.xxx.xxx
  15.     ServerName  xxx.xxx.xxx
  16.     ServerAlias  xxx.xxx.xxx 
  17.     DocumentRoot "/var/www/xxx.com" 
  18. </VirtualHost> 
  19. # ----------------------- end  ------------------------------ 

PS:

apache的log显示ip来源都是127.0.0.1,修改配置文件使其可以正常记录来访IP

修改前:


  1. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 

修改后


  1. LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 

快速清空cache


  1. echo "" > /usr/local/squid/var/cache/swap.state 

对于类似http://xxx.xxx.xxx/default.html之类的网页,squid只会生成一个缓存文件。可以用squid附带的squidclient工具清除:


  1. squidclient -m PURGE -p 80 "http://xxx.xxx.xxx/default.html" 

squid命中率查看


  1. squidclient -p 80 -U Henry -W xxxx mgr:info |grep -i hit 
  2.         Hits as % of all requests:      5min: 62.1%, 60min: 64.1% 
  3.         Hits as % of bytes sent:        5min: 63.4%, 60min: 60.6% 
  4.         Memory hits as % of hit requests:       5min: 97.3%, 60min: 89.7% 
  5.         Disk hits as % of hit requests: 5min: 0.2%, 60min: 0.1% 
  6.         Cache Hits:            0.00000  0.00000 
  7.         Near Hits:             0.00091  0.00091 

内存使用情况:


  1. squidclient -p 80 -U Henry -W password mgr:mem

该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户。


  1. tail -f /usr/local/squid/var/logs/access.log |grep TCP_MEM_HIT 
该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户。

  1. tail -f /usr/local/squid/var/logs/access.log |grep TCP_HIT  


该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户。


  1. tail -f /usr/local/squid/var/logs/access.log |grep TCP_MISS 

本文转自dongfang_09859 51CTO博客,原文链接:http://blog.51cto.com/hellosa/550567,如需转载请自行联系原作者