squid反向代理网站配置示例

简介:

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,如需转载请自行联系原作者
相关文章
|
应用服务中间件 nginx
Nginx反向代理/location/URL重写功能实战
一、Nginx反向代理常用实战 二、location 应用实例
Nginx反向代理/location/URL重写功能实战
|
应用服务中间件 nginx
JavaWeb - 获取客户端真实 IP(通过反向代理 Nginx)非服务器 IP(二)
JavaWeb - 获取客户端真实 IP(通过反向代理 Nginx)非服务器 IP(二)
375 0
|
应用服务中间件 Apache Windows
nginx基于客户端访问IP重定向
最近换了一家公司,工作比较忙,因为工作环境的关系导致不能经常写博客了,暂时手里也就写了LDAP+Samba+Vsftp统一集中认证项目文档、weblogic集群部署安装文档,由于各种关系导致不能外发,哈哈!各位博友原谅!现在就写一篇关于nginx的重定向文档吧! 项目需求: 公司现在的网站架构是nginx+apache反向代理,现需要将客户端访问的某两个网段IP地址重定向到应用主页,其余的所有IP地址访问都重定向到维护页面。
1425 0
|
9月前
|
应用服务中间件 nginx
宝塔 配置反向代理出现“伪静态/nginx主配置/vhost/文件已经存在全局反向代理
宝塔 配置反向代理出现“伪静态/nginx主配置/vhost/文件已经存在全局反向代理
904 0
宝塔 配置反向代理出现“伪静态/nginx主配置/vhost/文件已经存在全局反向代理
|
负载均衡 应用服务中间件 测试技术
nginx反向代理注意事项(/)
近期某项目测试环境进行迁移,迁移后的接口域名多了一个端口,出现了跨域问题,所以利用Nginx代理来解决这些问题。 proxy_pass的斜杠问题 Nginx的官网将proxy_pass分为两种类型:
118 0
|
应用服务中间件 定位技术 nginx
Nginx反向代理后无法获取客户端真实IP地址
当我们使用 Nginx 代理转发服务后,会发现我们无法获取客户端的真实IP地址,从而无法获取客户端的地理位置等信息。
578 0
|
应用服务中间件 nginx 开发者
Nginx反向代理/location/URL重写功能实战| 学习笔记
快速学习Nginx反向代理/location/URL重写功能实战。
167 0
Nginx反向代理/location/URL重写功能实战| 学习笔记
|
负载均衡 监控 应用服务中间件
什么是反向代理?Nginx反向代理如何配置?
nginx在日常工作中是一个不可缺少的服务,其中使用nginx做的事情最多的就是反反向代理,今天笔者带大家详细学习一下nginx反向代理。
703 2
什么是反向代理?Nginx反向代理如何配置?
|
应用服务中间件 nginx
|
网络协议 jenkins 应用服务中间件
如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析
如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析        前段时间公司根据要求需要将聚石塔上服务器从杭州整体迁移到张家口,刚好趁这次机会将这些乱七八糟的服务器做一次梳理和整合,断断续续一个月迁移完 成大概优化掉了1/3的机器,完成之后遇到了一些问题,比如曾今零零散散部署在生产上一些可视化UI:apollo,kibana,grafana,jenkins 等等要么采用80端口,要 么对公开放了其他端口,为了安全,现在不再开放非80之外的公网端口,由于机器少了,80端口不够,这些可视化UI不再能直接访问到了。
1229 0