Proxy

14,204 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
14,204
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
356
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Proxy

  1. 1. Squid Web Proxy Cache Server<br />
  2. 2. Proxy server’s web caching function<br />
  3. 3. Proxy location in a network<br />
  4. 4. Froxy different functions<br />Proxy caching<br />Network Address Translation<br />Filtering<br />
  5. 5. Benefits of proxy caching<br />
  6. 6. NAT Function<br />
  7. 7. Packet filter and Proxy Server<br />
  8. 8. System Requirements<br />Disk random seek time<br />For a proxy cache, make sure this number is as low as possible. The problem is that operating systems try to speed up disk access times using various methods that usually slow the system’s performance <br />Amount of system memory<br />RAM is also extremely important when using a proxy cache. Squid keeps an in-memory table of its objects in RAM, which should always remain in RAM. If part of the table goes to swap, the performance of Squid is greatly degraded. <br />
  9. 9. Download and Install The Squid Package<br />Download the latest stable version of Squid (www.squid-cache.org) <br />Install the RPM by using the rpm –i command. <br />
  10. 10. Lưu ý khi Cài đặt Squid<br />Sau khi cài đặt lại Squid là một chương trình thay vì là một dịch vụ.<br />Trước khai cài đặt tạo phân vùng /cache<br />Chạy dòng lệnh trong terminal (phải có quyền root) <br /># useradd -d /cache/ -r -s /dev/null squid<br />Giải nén gói cài đặt squid-2.4.STABLE1-src.tar.gz<br /># tar xzpf squid-2.4.STABLE1-src.tar.gz<br />
  11. 11. Lưu ý khi Cài đặt Squid<br />Di chuyển đến thư mục vừa giải nén của Squid và cấu hình squid bật chức năng delay pools trước khi cài đặt<br />./configure --prefix=/opt/squid --exec-prefix=/opt/squid --enable-delay-pools --enable-cache-digests --enable-poll --disable-ident-lookups --enable-truncate --enable-removal-policies<br /># make all<br /># make install<br />
  12. 12. Squid: Starting and stopping<br /># /etc/init.d/squid stop<br /># /etc/init.d/squid start<br /># /etc/init.d/squid restart<br /># /etc/init.d/squid reload<br />
  13. 13. Squid: LogFiles<br />/var/log/squid/cache.logContains run-time status messages, warnings, and errors.<br />/var/log/squid/access.logOne line for each client request, including URL, bytes trans-ferred, status code, and more.<br />/var/log/squid/store.logTransaction log for objects that enter and leave the cache.<br />Open a new terminal window and run:$ tail -f /var/log/squid/cache.log<br />Open another new terminal window and run:$ tail -f /var/log/squid/access.log<br />
  14. 14. Configuring: Cache Disks<br />The cache dir directive(s) tell Squid how and where to store cached objects.<br />cache_dir type path megabytes L1 L2<br />cache_dirufs /var/spool/squid 100 16 256<br />The default typeis ufs, but aufs has better performance on Linux.<br />pathcan be anywhere on the filesystem, but is usually a dedicated disk or partition.<br />megabytesis an upper limit on how much space Squid should use for this cachedir. It should be less than 90% of the actual capacity.<br />L1and L2specify the number of first- and second-level directories to use. Use 16 and 256 by default. These should not be changed after Squid has placed objects on the disk.<br />
  15. 15. The cache directory structure for ufs-based storage schemes <br />
  16. 16. Squid: Create Swap Directories<br />After adding a cache dir , you need to initialize it with this command:<br /># squid -z<br />2006/10/12 09:48:24| Creating Swap Directories<br />Ownership and permissions are a common problem at this stage. Squid runs under a certain user ID, specified with cache_effective_user in squid.conf. This user ID must have read and write permission under each cache_dir directory. If not, you&apos;ll see a message like this:<br />Creating Swap Directories FATAL: Failed to make swap directory /usr/local/squid/var/cache/00: (13) Permission denied <br />In this case, you should make sure that all components of /usr/local/squid/var/cache are accessible to the user ID given in squid.conf. The final component—the cache directory—must be writable by this user ID as well.<br />
  17. 17. Check Your Configuration File for Errors <br />Before trying to start Squid, you should verify that your squid.conf file makes sense. This is easy to do. Just run the following command:<br /># squid -k parse<br />If you see no output, the configuration file is valid, and you can proceed to the next step.<br />However, if your configuration file contains an error, Squid tells you about it:<br />squid.conf line 62: http_access allow okay2<br />aclParseAccessLine: ACL name &apos;okay2&apos; not found.<br />Here you can see that the http_access directive on line 62 references an ACL that doesn&apos;t exist. Sometimes the error messages are less informative:<br />FATAL: Bungled squid.conf line 76: memory_pools<br />In this case, we forgot to put either on or off after the memory_pools directive on line 76.<br />
  18. 18. Configuring: User ID<br />Unfortunately, running Squid isn&apos;t always so simple. In some cases, you may need to start Squid as root, depending on your configuration. For example, only root can bind a TCP socket to privileged ports like port 80. If you need to start Squid as root, you must set the cache_effective_user directive. It tells Squid which user to become after performing the tasks that require special privileges. For example:<br />cache_effective_user squid <br />If you start Squid as root without setting cache_effective_user, Squid uses nobody as the default value. Whatever user ID you choose for Squid, make sure it has read access to the files installed in $prefix/etc, $prefix/libexec, and $prefix/share. The user ID must also have write access to the log files and cache directory.<br />
  19. 19. Configuring: Port Numbers <br />The http_port directive tells Squid which port number to listen on for HTTP requests. The default is port 3128:<br />http_port 3128<br />Youcan instruct Squid to listen on multiple ports with additional http_port lines.For example, the browsers from one department may be sending requests to port 3128, while another department uses port 8080. Simply list both port numbers as follows:<br />http_port 3128<br />http_port 8080<br />You can also use the http_port directive to make Squid listen on specific interface addresses, simply put the IP address in front of the port number:<br />http_port 192.168.1.1:3128<br />
  20. 20. Configuring: Visible Hostname <br />Squid wants to be sure about its hostname for a number of reasons:<br />The hostname appears in Squid&apos;s error messages. This helps users identify the source of potential problems.<br />The hostname appears in the HTTP Via header of cache misses that Squid forwards. When the request arrives at the origin server, the Via header contains a list of all proxies involved in the transaction. Squid also uses the Via header to detect forwarding loops. <br />Squid uses internal URLs for certain things, such as the icons for FTP directory listings. When Squid generates an HTML page for an FTP directory, it inserts embedded images for little icons that indicate the type of each file in the directory. The icon URLs contain the cache&apos;s hostname so that web browsers request them directly from Squid.<br />Each HTTP reply from Squid includes an X-Cache header. <br />Syntax: visible_hostname squid.hcmuaf.edu.vn<br />
  21. 21. Quid: ACLs<br />ACL elements are the building blocks of Squid&apos;s access control implementation. These are how you specify things such as IP addresses, port numbers, hostnames, and URL patterns. Each ACL element has a name, which you refer to when writing the access list rules. <br />acl name type value1 value2 ...<br />For example:acl Workstations src 10.0.0.0/16 <br />In most cases, you can list multiple values for one ACL element. You can also have multiple acl lines with the same name. For example, the following two configurations are equivalent:<br />acl Http_ports port 80 8000 8080 <br />acl Http_ports port 80 <br />acl Http_ports port 8000 <br />acl Http_ports port 8080 <br />
  22. 22. ACL type: IP Address<br />Used by: src, dst<br />Squid has a powerful syntax for specifying IP addresses in ACLs. You can write addresses as subnets, address ranges, and domain names. Squid supports both &quot;dotted quad&quot; and CIDR prefix subnet specifications. In addition, if you omit a netmask, Squid calculates the appropriate netmask for you. For example, each group in the next example are equivalent:<br />acl Foo src 172.16.44.21/255.255.255.255 acl Foo src 172.16.44.21/32 acl Foo src 172.16.44.21 <br />acl Xyz src 172.16.55.32/255.255.255.248 acl Xyz src 172.16.55.32/28 <br />acl Bar src 172.16.66.0/255.255.255.0 acl Bar src 172.16.66.0/24 acl Bar src 172.16.66.0<br />You can also specify hostnames in IP ACLs. acl Squid dst www.squid-cache.org <br />
  23. 23. ACL type: domain name<br />Used by: srcdomain, dstdomain, and the cache_host_domain directive<br />A domain name is simply a DNS name or zone. For example, the following are all valid domain names:<br />www.squid-cache.org, squid-cache.org, org<br />Domain name matching can be confusing, so let&apos;s look at another example so that you really understand it. Here are two slightly different ACLs:<br />acl A dstdomain foo.com <br />acl B dstdomain .foo.com <br />A user&apos;s request to get http://www.foo.com/ matches ACL B, but not A. ACL A requires an exact string match, but the leading dot in ACL B is like a wildcard.<br />On the other hand, a user&apos;s request to get http://foo.com/ matches both ACLs A and B. Even though there is no word before foo.com in the URL hostname, the leading dot in ACL B still causes a match.<br />
  24. 24. ACL type: Regular expressions <br />Used by: srcdom_regex, dstdom_regex, url_regex, urlpath_regex, browser, referer_regex, ident_regex, proxy_auth_regex, req_mime_type, …<br />A number of ACLs use regular expressions (regex) to match character strings. For Squid, the most commonly used regex features match the beginning and/or end of a string. For example, the ^ character is special because it matches the beginning of a line or string:<br />^http://This regex matches any URL that begins with http://. The $ character is also special because it matches the end of a line or string:<br />.jpg$<br />With all of Squid&apos;s regex types, you have the option to use case-insensitive comparison. Matching is case-sensitive by default. To make it case-insensitive, use the -i option after the ACL type. For example:<br />acl Foo url_regex -i ^http://www<br />
  25. 25. ACL types: TCP port numbers <br />Used by: port, myport<br />This type is relatively straightforward. The values are individual port numbers or port number ranges. Recall that TCP port numbers are 16-bit values and, therefore, must be greater than 0 and less than 65,536. Here are some examples:<br />acl Foo port 123 <br />acl Bar port 1-1024 <br />acl Safe_ports port 443 563 <br />
  26. 26. ACL type: time <br />The time ACL allows you to control access based on the time of day and the day of the week. The syntax is somewhat cryptic:<br />acl name [days] [h1:m1-h2:m2] <br />You can specify days of the week, starting and stopping times, or both. Days are specified by the single-letter codes:<br />S:Sunday; M:Monday; T: Tuesday; W: Wednesday; H: Thursday; F: Friday; A: Saturday; D: All weekdays (M-F)<br />Times are specified in 24-hour format. The starting time must be less than the ending time, which makes it awkward to write time ACLs that span &quot;midnights.&quot;<br />acl Working_hours MTWHF 08:00-17:00 or: acl Working_hours D 08:00-17:00<br />acl Offpeak1 20:00-23:59 <br />acl Offpeak2 00:00-04:00<br />
  27. 27. Access Control Rules: http_access Tag<br />The http_access tag permits or denies access to Squid. You can allow or deny all requests. You can also allow or deny requests based on a defined access list. If you remove all of the http_access entries, all requests are allowed by default. <br />NOTE: Squid should never be used without some type of authentication system or access control list. You must restrict Internet users from relaying requests through your Web proxy cache. <br />Syntax:<br /> http_accessallow|deny[!]aclname [aclname] ...<br />http_access allow Net1 WorkingHours <br />http_access allow Net2 WorkingHours <br />http_access allow Net4 <br />http_access deny All<br />
  28. 28. Squid authentication <br />1) Create the password file. The name of the password file should be /etc/squid/squid_passwd, and you need to make sure that it&apos;s universally readable. <br /># touch /etc/squid/squid_passwd<br /># chmod o+r /etc/squid/squid_passwd<br />2) Use the htpasswd program to add users to the password file. You can add users at anytime without having to restart Squid. In this case, you add a username called www: <br /># htpasswd /etc/squid/squid_passwd www<br />New password:<br />Re-type new password:<br />Adding password for user www<br />3) Find your ncsa_auth file using the locate command. <br /># locate ncsa_auth<br />/usr/lib/squid/ncsa_auth<br />
  29. 29. Squid authentication<br />4) Edit squid.conf; specifically, you need to define the authentication program in squid.conf, which is in this case ncsa_auth. Next, create an ACL named ncsa_users with the REQUIRED keyword that forces Squid to use the NCSA auth_param method you defined previously. Finally, create an http_access entry that allows traffic that matches the ncsa_users ACL entry. Here&apos;s a simple user authentication example; the order of the statements is important: <br />#Add this to the auth_param section of squid.conf<br />auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_passwd<br /># Add this to the bottom of the ACL section of squid.conf<br />acl ncsa_users proxy_auth REQUIRED<br /># Add this at the top of the http_access section of squid.conf<br />http_access allow ncsa_users<br />
  30. 30. Squid authentication<br />5) This requires password authentication and allows access only during business hours. Once again, the order of the statements is important: <br /># Add this to the auth_param section of squid.conf<br />auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_passwd<br /># Add this to the bottom of the ACL section of squid.conf<br />acl ncsa_users proxy_auth REQUIRED<br />acl business_hours time M T W H F 9:00-17:00<br /># Add this at the top of the http_access section of squid.conf<br />http_access allow ncsa_users business_hours<br />
  31. 31. Scenarios: Restricting Web Access By Time <br /># Add this to the bottom of the ACL section of squid.confacl home_network src 192.168.1.0/24<br />acl business_hours time M T W H F 9:00-17:00<br />acl RestrictedHost src 192.168.1.23<br /># Add this at the top of the http_access section of squid.conf<br />http_access deny RestrictedHost<br />http_access allow home_network business_hours<br /># Or, you can allow morning access only: <br /># Add this to the bottom of the ACL section of squid.conf<br />acl mornings time 08:00-12:00<br /># Add this at the top of the http_access section of squid.conf<br />http_access allow mornings<br />
  32. 32. Scenarios: Restricting Access to specific Web sites <br />Squid is also capable of reading files containing lists of web sites and/or domains for use in ACLs. In this example we create to lists in files named /usr/local/etc/allowed-sites.squid and /usr/local/etc/restricted-sites.squid. <br />#File:/usr/local/etc/allowed-sites.squid<br /> www.openfree.org<br /> Linuxhomenetworking.com<br /># File: /usr/local/etc/restricted-sites.squid<br /> www.porn.com<br /> illegal.com<br />
  33. 33. Scenarios: Restricting Access to specific Web sites<br />These can then be used to always block the restricted sites and permit the allowed sites during working hours. This can be illustrated by expanding our previous example slightly. <br /># Add this to the bottom of the ACL section of squid.conf<br />acl home_network src 192.168.1.0/24<br />acl business_hours time M T W H F 9:00-17:00<br />acl GoodSites dstdomain &quot;/usr/local/etc/allowed-sites.squid&quot;<br />acl BadSites dstdomain &quot;/usr/local/etc/restricted-sites.squid&quot;<br /># Add this at the top of the http_access section of squid.conf<br />http_access deny BadSites<br />http_access allow home_network business_hours GoodSites<br />
  34. 34. Configuring Squid<br />The visible_hostname Tag<br />Squid will fail to start if you don&apos;t give your server a hostname. You can set this with the &quot;visible_hostname&quot; parameter. visible_hostname bigboy<br />The http_port Tag<br />The http_port tag configures the HTTP port on which Squid listens for proxy clients. Default port is 3128. We can configure Squid to listen on ports 3128 and 8080 for proxy clients.http_port 3128 8080<br />The Cache_dir Tag<br />The cache_dir tag specifies where the cached data is stored. By default, the following cache_dir tag value is presented:cache_dirufs /var/spool/squid 100 16 256<br />
  35. 35. Defining the Default cache_dir tag <br />
  36. 36. Configuring the acl Tag<br />aclaclnamesrcip-address/netmask ... (clients IP address)<br />aclaclnamesrcaddr1-addr2/netmask ... (range of addresses)<br />aclaclnamedstip-address/netmask ... (URL host&apos;s IP address)<br />aclaclnamesrcdomain .foo.com ... reverse lookup, client IP<br />aclaclnamedstdomain .foo.com ... Destination server from URL<br />aclaclnameurl_regex [-i] ^http://… regex matching on whole URL<br />aclaclnameurlpath_regex [-i] .gif$... regex matching on URL path<br />
  37. 37. Configuring the acl Tag<br />aclaclnameport807021<br />aclaclnameport0-1024...ranges allowed<br />aclaclnameprotoHTTPFTP ...<br />aclaclnamemethodGETPOST ...<br />aclaclnametime [day] [h1:m1-h2:m2]<br />day:<br />S - Sunday<br />M - Monday<br />T - Tuesday<br />W - Wednesday<br />H - Thursday<br />F - Friday<br />A - Saturday<br />h1:m1 must be less than h2:m2<br />aclhome_networksrc192.168.1.0/24<br />aclbusiness_hourstimeM T W H F 9:00-17:00<br />
  38. 38. Recommended minimum configuration<br />acl all src 0.0.0.0/0.0.0.0<br />acl manager proto cache_object<br />acl localhost src 127.0.0.1/255.255.255.255<br />acl to_localhost dst 127.0.0.0/8<br />acl SSL_ports port 443 563<br />acl Safe_ports port 80 # http<br />acl Safe_ports port 21 # ftp<br />acl Safe_ports port 443 563 # https, snews<br />acl Safe_ports port 70 # gopher<br />acl Safe_ports port 210 # wais<br />acl Safe_ports port 1025-65535 # unregistered ports<br />acl Safe_ports port 280 # http-mgmt<br />acl Safe_ports port 488 # gss-http<br />acl Safe_ports port 591 # filemaker<br />acl Safe_ports port 777 # multiling http<br />acl CONNECT method CONNECT<br />
  39. 39. The http_access Tag<br />The http_access tag permits or denies access to Squid. You can allow or deny all requests. You can also allow or deny requests based on a defined access list. If you remove all of the http_access entries, all requests are allowed by default. <br />Proxy clients will be unable to use the Squid proxy-caching server until you modify the http_access tags. Please note that some level of access control is recommended, so do not remove all of the http_access tags. <br />NOTE: Squid should never be used without some type of authentication system or access control list. You must restrict Internet users from relaying requests through your Web proxy cache. <br />Syntax:http_accessallow|deny[!]aclname [aclname] ...<br />
  40. 40. Recommended minimum configuration<br />http_accessallowmanagerlocalhost<br />http_accessdenymanager<br />http_accessdeny!Safe_ports<br />http_accessdenyCONNECT!SSL_ports<br /># INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTSFor example:http_accessallowhome_networkbusiness_hours<br />http_accessallowlocalhost<br />http_accessdenyall<br />
  41. 41. The icp_port tag<br />The icp_port tag:<br /> Internet Cache Protocol (ICP) : Queries other caches for a specific objecticp_port: The port number where Squid sends and receives ICP queries to and from neighbor caches. Default is 3130. To disable use &quot;0&quot;.<br />icp_port 8082<br />The cache_peer tag:<br /> To specify other caches in a hierarchy, use the format:<br /> cache_peer hostname type http_port icp_port<br /> For example<br />proxy icp<br /> hostname type port port options<br /> -------------------- -------- ----- ----- -----------<br />cache_peerproxy2.hcmuaf.edu.vnparent80808082<br />cache_peerproxy.kcntt.hcmuaf.edu.vnsibling80808082<br />Type:<br /> ‘parent’ : parent proxy in higher level ‘sibling’: peer proxy <br />
  42. 42. Configuring Proxy Clients (IE)<br />Open Internet Explorer.<br />Click the Tools menu and choose Internet Options. <br />Select the Connections tab, and click LAN Settings. <br />Deselect Automatically Detect Setting. <br />In the Proxy server section, click the Use a proxy server check box. <br />In the Address field, enter the IP address of your Squid Web Proxy Cache server. <br />In the Port field, enter port 8080 <br />Click OK twice to return to the browser. <br />In Internet Explorer, enter the following URL: www.squid-cache.org.<br />The Squid home page will appear. If not, your browser proxy settings are incorrectly configured.<br />
  43. 43. Configuring Proxy Clients (IE)<br />
  44. 44. Forcing Users To Use Your Squid Server<br />This is called a &quot;transparent proxy&quot; configuration. It is usually achieved by configuring a firewall between the client PCs and the Internet to redirect all HTTP (TCP port 80) traffic to the Squid server on TCP port 3128 (which is Squid server default TCP port).<br />In both cases below:<br />The firewall is connected to the internet on interface eth0 and to the home network on interface eth1.<br />The firewall is the default gateway for the home network which uses NAT to access the Internet.<br />Only the squid server has access to the internet on port 80 (HTTP). This happens because all HTTP traffic, except that coming from the squid server, is redirected.<br />
  45. 45. Firewall configuration<br />Squid Server And Firewall Are The Same ServerHere all HTTP traffic from the home network is redirected to the firewall itself on the squid port of 3128.<br /> iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128<br /> iptables -A OUTPUT -j ACCEPT -m state --state NEW -o eth0 -p tcp --dport 80<br />Squid Server And Firewall Are Different ServersHere all HTTP traffic from the home network except from the squid server at IP address 192.168.1.100 is redirected to the Squid server on the squid port of 3128. <br /> iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j DNAT --to 192.168.1.100:8080 -s ! 192.168.1.100/32<br /> iptables -A OUTPUT -j ACCEPT -m state --state NEW -o eth0 -p tcp --dport 80<br />
  46. 46. Summary<br />Benefits of Proxy Server Implementation<br /> A Web proxy cache server can cache Web pages and FTP files for proxy clients. They can also cache Web sites for load balancing. <br /> Caching increases the performance of the network by decreasing the amount of data transferred from outside of the local network.<br /> Web proxy caching reduces bandwidth costs, increases network performance during normal traffic and spikes, performs load balancing, caches aborted requests, and functions even when a network’s Internet connection fails. <br />Differentiating between a Packet Filter and a Proxy Server<br /> Packet filters analyze traffic at the Network (Layer 3) and Transport layers (Layer 4) of the OSI model. A packet filter can determine whether it will allow a certain IP address or IP address range to pass through, or filter traffic by service, or port number.<br /> A proxy server analyzes packets at the Application layer (Layer 7) of the OSI model. This feature provides flexibility because the traffic within one service, such as port 80 (HTTP) traffic, can be filtered.<br />
  47. 47. Summary<br />Implementing the Squid Web Proxy Cache Server<br /> The Squid Web Proxy Cache server allows administrators to set up a Web proxy caching service, add access controls (rules), and cache DNS lookups. <br /> Client protocols supported by Squid must be sent as a proxy request in HTTP format, and include FTP, HTTP, SSL, WAIS, and Gopher. <br /> Squid is configured using the /etc/squid/squid.conf file, which defines configurations such as the HTTP port number on which Squid listens for HTTP requests, incoming and outgoing requests, timeout information, and firewall access data. <br /> Each configuration option in squid.conf is identified as a tag. The http_port tag configures the HTTP port on which Squid listens for proxy clients. The cache_dir tag specifies where the cached data is stored. The acl tag allows you to define an access list. The http_access tag permits or denies access to Squid. Squid will not function until you make changes to the squid.conf file. <br />

×