Chapter 1 configure Squid
1. Set the hosts table
First, we need to understand the role of squid in CDN. Squid is actually a proxy + cache in CDN. Like a common proxy server, squid takes the place of a user to request a server with real content and caches it. The difference is that squid exists as a web server similar to apache. When users access it, they do not feel that they have accessed a proxy server, but a real web server. Generally, this method is called "reverse proxy ".
Therefore, we need to let squid know where to fetch the content. Because we have set up intelligent DNS for the domain name naizhao.com, and this squid server is also Netcom's, according to normal dns resolution, resolve naizhao.com to 2.2.2.2 (that is, the Netcom machine itself ). However, this IP address does not have any required content. All required content is actually on 1.1.1.1 (source server ). So we need to fix an IP address for naizhao.com, that is, 1.1.1.1.
There are two ways for the system to obtain this IP Address: one is to set up the hosts table, and the other is to build a dns server in the system. In Windows, you should always edit the hosts file. This file also exists in linux, which we mentioned in the previous vim tutorial. The method for setting up a dns system is too large, and it is relatively easy to edit the hosts file, so we adopt the method of editing the hosts table.
The hosts table in linux is located in/etc/hosts. The format is roughly the same as that in Windows. When it is opened, we add the following line to the end.
1.1.1.1 naizhao.com www.naizhao.com cnc.naizhao.com
If you have added the 1.1.1.1 record according to the previous tutorial, change it to the above format.
2. Set squid
The squid configuration file is located in/usr/local/squid/etc/squid. conf. The configuration file is displayed in English. For the moment, we do not need to understand the meaning of each configuration in this file. I believe you will see a lot of English. You only need to know how to run squid as needed without making any mistakes.
Run the rm command to delete squid. conf. You don't have to worry about finding it after deleting it. There is also a default squid. conf. default. In general linux programs, including apache, there will be a. default file to help you restore the default settings, or as a reference for modification.
After squid. conf is deleted, we use vi squid. conf to create the file, post the following content, and save it.
Http_port 80 vhost vport = 80
Acl apache rep_header Server ^ Apache
Broken_vary_encoding allow apache
Cache_mem 1024 MB
Maximum_object_size 4096 KB
Minimum_object_size 0 KB
Maximum_object_size_in_memory 256 KB
Cache_dir ufs/var/spool/squid 20480 16 256
Cache_swap_low 80
Cache_swap_high 97
Strip_query_terms off
Request_body_max_size 5 MB
Memory_pools on
Memory_pools_limit 50 MB
Access_log none
Cache_log/var/log/squid/cache. log
Cache_store_log none
Pid_filename/var/run/squid. pid
Hosts_file/etc/hosts
Refresh_pattern ^ ftp: 1440 20% 10080
Refresh_pattern ^ gopher: 1440 0% 1440
Refresh_pattern-I naizhao \. com 240 100% 1440 ignore-reload
Refresh_pattern-I www \. naizhao \. com 240 100% 1440 ignore-reload
Refresh_pattern-I. gif 180 20% 10080 override-expire ignore-reload-into-ims
Refresh_pattern-I. jpg 180 20% 10080 override-expire ignore-reload-into-ims
Refresh_pattern. 120 50% 1440
Acl all src 0.0.0.0/0.0.0.0
Acl p-manager proto cache_object
Acl s-localhost src 127.0.0.1/255.255.255.255
Acl d-localhost dst 127.0.0.0/8
Acl d-domains dstdomain .naizhao.com
Acl p-ssl port 443 563
Acl p-safe port 80 443 563
Acl m-conn method CONNECT
Acl m-purge method PURGE
Acl n-maxconn 15
Http_access allow p-manager s-localhost
Http_access allow m-purge
Http_access deny! P-safe
Http_access deny m-conn! P-ssl
Http_access deny n-maxconn
Http_access allow p-manager
Http_access allow d-domains
Http_access deny all
Http_reply_access allow all
Acl r-url urlpath_regex realtime
Cache deny r-url
Icp_access allow all
Follow_x_forwarded_for allow all
Acl_uses_indirect_client offrange_offset_limit-1
Dns_timeout 2 seconds
Forward_timeout 10 seconds
Connect_timeout 10 seconds
Peer_connect_timeout 6 seconds
Read_timeout 10 seconds
Request_timeout 6 seconds
Persistent_request_timeout 16 seconds
Cache_mgr silence.z@gmail.com
Cache_inclutive_user nobody
Cache_inclutive_group nobody
Visible_hostname cnc.naizhao.com
Logfile_rotate 0
Always_direct allow all
Error_directory/usr/local/squid/share/errors/Simplify_Chinese
Coredump_dir/var/spool/squid
Parameters to be described:
Cache_mem 1024 MB (the memory size used by squid. Note that about MB of memory must be left for the system. We recommend that you allocate 2 GB of system memory and 1 GB of memory to squid)
Cache_dir ufs/var/spool/squid 20480 16 256 (the cache directory is placed under/var/spool/squid. The size is 20 GB and the first level directory is 16, the second-level directory is 256. Because there is a limit on the number of files and directories placed under a directory, multiple directories must be set here to meet squid's need to store a large number of files. Do not set the directory too large. Otherwise, the system will crash)
Memory_pools_limit 50 MB (memory pool, used to store the linked list of cached files, which facilitates fast searching for the corresponding location of the file storage, and does not need to be too large. Don't worry)
Cache_log/var/log/squid/cache. log (the location where the log file is stored must have the read and write permissions. I will talk about it later)
Refresh_pattern-I naizhao \. com 240 100% 1440 ignore-reload
Refresh_pattern-I www \. naizhao \. com 240 100% 1440 ignore-reload
Refresh_pattern-I. gif 180 20% 10080 override-expire ignore-reload-into-ims
Refresh_pattern-I. jpg 180 20% 10080 override-expire ignore-reload-into-ims
(The above lines specify the File Cache Time. You can specify a domain name or a file type. Add more domain names or file types as needed. -I is a little bit written. naizhao.comand .gif are file types, 240 is the minimum Cache Time, and 1440 is the maximum cache time, which is minutes. You don't need to worry about this. You can adjust it when necessary. Our slogan is: Run first)
Acl d-domains dstdomain .naizhao.com (list of domain names that can be accessed. If you want to run multiple websites, add more than a few and separate them with spaces. Naizhao.com is preceded by an English., indicating that all * .naizhao.com is allowed. If you do not have this point, only naizhao.com is allowed. Access to www.naizhao.com will prompt that the access is denied .)
Cache_mgr silence.z@gmail.com (administrator's email address, changed to your own)
Cache_inclutive_user nobody
Cache_inclutive_group nobody
(Used to run squid users and user groups. To avoid security issues caused by using the root account, most linux programs run with root, and then immediately switch the running users from root to other users. In this way, even if the program has vulnerabilities hacked, hackers can only obtain the permissions of this common user, which greatly reduces the harm to the system. Keep the default value)
Visible_hostname cnc.naizhao.com)
Coredump_dir/var/spool/squid (where will the last words be put after squid fails. Don't worry, most people cannot understand)
After modifying the configuration file, we still need to do some final work.
Create a log directory to store log files. According to the settings of squid. conf, we enter the following command
Mkdir/var/log/squid
Chown nobody: nobody/var/log/squid
The chown command is used to set the owner and group. As mentioned earlier, squid will run with the user's nobody group nobody. Nobody: the user is prior to the nobody colon, and the user group is followed.
The squid cache directory also needs to be set to nobody: nobody
Chown nobody: nobody/var/spool/squid
Run the ls-lh command to check whether the command is successful.
Ls-lh/var/spool/
......
Drwxr-xr-x 3 nobody 4.0 K Jun 15 squid
The last step is to initialize the cache directory structure of squid. (That is, 16 level-1 directories and 256 level-2 directories. These folders do not exist and need to be created using the squid command)
The squid command is under/usr/local/squid/sbin. Let's go to this directory and check the help of the squid command.
./Squid-h
Usage: squid [-hvzCDFNRYX] [-d level] [-s |-l facility] [-f config-file] [-u port] [-k signal]
-D level Write debugging to stderr also.
-F file Use given config-file instead
/Usr/local/squid/etc/squid. conf
-H Print help message.
-K reconfigure | rotate | shutdown | interrupt | kill | debug | check | parse
Parse configuration file, then send signal
Running copy (skip T-k parse) and exit.
-S |-l facility
Enable logging to syslog.
-U port Specify ICP port number (default: 3130), disable with 0.
-V Print version.
-Z Create swap directories
-C Do not catch fatal signals.
-D Disable initial DNS tests.
-F Don't serve any requests until store is rebuilt.
-N No daemon mode.
-R Do not set REUSEADDR on port.
-S Double-check swap during rebuild.
-X Force full debugging.
-Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.
We can see that the-z command creates a cache directory, So we enter
./Squid-z
07:52:03 | Creating Swap Directories
No error message indicates that the directory has been successfully created. You can use ls/var/spool/squid to check the effect.
Finally, let squid run.
./Squid-Nd 10
07:53:50 | Starting Squid Cache version 2.6.STABLE13 for i686-pc-linux-gnu...
2007/07/07 07:53:50 | Process ID 3135
07:53:50 | With 1024 file descriptors available
07:53:50 | Using poll for the IO loop
07:53:50 | Deming DNS Tests...
07:53:50 | Successful DNS name lookup tests...
07:53:50 | DNS Socket created at 0.0.0.0, port 1026, FD 4
07:53:50 | Adding nameserver 211.147.6.3 from/etc/resolv. conf
07:53:50 | Adding nameserver 61.233.9.9 from/etc/resolv. conf
07:53:50 | Unlinkd pipe opened on FD 8
07:53:50 | Swap maxSize 20971520 KB, estimated 1613193 objects
07:53:50 | Target number of buckets: 80659
07:53:50 | Using 131072 Store buckets
07:53:50 | Max Mem size: 1048576 KB
07:53:50 | Max Swap size: 20971520 KB
2007/07/07 07:53:50 | Store logging disabled
07:53:50 | Rebuilding storage in/var/spool/squid (DIRTY)
07:53:50 | Using Least Load store dir selection
07:53:50 | Set Current Directory to/var/spool/squid
07:53:50 | Loaded Icons.
07:53:52 | Accepting accelerated HTTP connections at 0.0.0.0, port 80, FD 7.
07:53:52 | Accepting ICP messages at 0.0.0.0, port 3130, FD 9.
07:53:52 | WCCP Disabled.
07:53:52 | Ready to serve requests.
07:53:52 | Done scanning/var/spool/squid (0 entries)
07:53:52 | Finished rebuilding storage from disk.
07:53:52 | 0 Entries scanned
07:53:52 | 0 Invalid entries.
07:53:52 | 0 With invalid flags.
07:53:52 | 0 Objects loaded.
07:53:52 | 0 Objects expired.
07:53:52 | 0 Objects canceled.
07:53:52 | 0 Duplicate URLs purged.
07:53:52 | 0 Swapfile clashes avoided.
07:53:52 | Took 2.0 seconds (0.0 objects/sec ).
07:53:52 | Beginning Validation Procedure
07:53:52 | Completed Validation Procedure
07:53:52 | Validated 0 Entries
07:53:52 | store_swap_size = 0 k
07:53:53 | storeLateRelease: released 0 objects
Open the browser and enter http: // 2.2.2.2. The error message "Access Denied" is displayed, indicating that squid is running properly.
Because the-d parameter is specified, squid will output all debugging information to the terminal. Press ctrl + c on the keyboard and enter killall squid to kill the squid process.