Building Edge Server With Squid 2.6

I have built a few edge servers using squid, one was discussed here. I just realized that I haven’t wrote down how I did it. So here it is :

note: squid 2.6 introduce a new syntax for httpd acceleration. This guide is specific to squid 2.6 or later. (httpd_accel directives are not used in this guide)

1. Install squid

Prefereably using your distro’s package management software (example: “aptitude install squid” on Debian-based distro, or “yum install squid” on Fedora)

But in certain cases you may wish to download the latest version, which you may download from http://www.squid-cache.org/Download/
When I wrote this, 2.6 is the latest stable version.

But you may wish to install version 3.0 instead, for example; if you’re going to install squid as a proxy server, and you wish to save bandwidth by making all of its outgoing requests to specify compression.
However, at this time, squid version 3.0 is not of production quality yet. You have been warned.

Anyway, if you install squid from source, it’s usually as easy as :

mkdir /usr/local/squid
./configure –prefix=/usr/local/squid
make all
make install

## prepare directories
mkdir /usr/local/squid/var/cache
mkdir /usr/local/squid/var/logs
## setup the correct permissions
chown -R nobody:nobody /usr/local/squid/var/logs/
chown -R nobody:nobody /usr/local/squid/var/cache/

2. Setup squid.conf

Here’s the minimum settings required to make squid work as an edge server :

http_port 80 accel defaultsite=www.myaccelerateddomain.com vhost
cache_peer www.myaccelerateddomain.com parent 1234 0 no-query originserver login=PASS
http_access allow all
icp_access allow all
### Disk cache: 4096 MB, 16 top directories max, 256 second-level directories max
cache_dir ufs /usr/local/squid/var/cache 4096 16 256

You may need to change more options, but in most cases, these would be enough.

Some optimizations :

== cache_mem 512 MB : set the cache memory to 512 MB. Adjust to your own situation.
== collapsed_forwarding on : imagine when there are 50 requests for the same page that’s not in the squid’s cache yet. Normally, squid will pass all of them to the webserver. But with this option turned on, squid will pass just one request to the webserver, get the result, and then reply to all 50 of them. Very nice.
== maximum_object_size 4096 KB : More than this, and we’ll be using up the cache disk space faster than we’d like.
== maximum_object_size_in_memory 1024 KB : More than this, and we’ll be using up the cache memory faster than we’d like.
== access_log /usr/local/squid/var/logs/access.log combined : this gives most details in the logfile, but will eat up disk space faster

3. Move webserver from port 80

In the squid.conf above, we specified that the webserver will be listening on port 1234 instead.
So make the necessary adjustments to your webserver’s settings.

4. Restart webserver, and then start squid

Restart your webserver, and then start squid with :

## create the cache directories first
/usr/local/squid/sbin/squid -z
## start squid
/usr/local/squid/sbin/squid

5. Done !

That’s it, now squid will be answering all the requests for your webserver, and will only forward the requests to the webserver if necessary.

Enjoy.

6. Problems ?

  • Some Linux distros by default can only have max 1024 files opened simultaneously. When you have squid running in a busy server, this limit can be very quickly exceeded. When that happened, your server will lock up in a rather spectacular way. Yes, you definitely don’t want this to happen to your server.

    Fortunately, this can be easily fixed, by typing ulimit -n [some numbers]. Example; ulimit -n 4000 will increase the open file limit to 4000.

  • Make it all automatic : To avoid doing these again and again, insert the following lines in the startup script (probably /etc/rc.local or something like that) :

    ulimit -n 4000
    /usr/local/squid/sbin/squid

  • Squid still will NOT cache your pages?
    Sometimes this can be caused by lack of any hint from webserver/PHP, making squid unsure whether to cache this page or not — and to be on the safe side, it default to NOT caching the page.

    To assure squid that it’s okay to cache, put the following lines in the right place of your Apache configuration file :

    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault “access plus 1 week”
    </IfModule>

Finally, enjoy ! Your server will now serve incoming slashdotting / digg / other kind of massive incoming traffic without breaking a sweat.

51 thoughts on “Building Edge Server With Squid 2.6

  1. @dikshie – thanks, tapi dengan squid saja saya sudah bisa mendapatkan performance gain sampai lebih dari 10x 🙂
    Dan, yang paling penting, squid sudah proven soal reliability.
    .
    Jadi kalau untuk server production, saya pasti pilih yang aman.
    .
    Anyway terimakasih banyak untuk informasinya, mudah2an varnish bisa menjadi alternatif yang bahkan lebih baik lagi daripada squid.

  2. For those using Squid 2.5 (or before), here’s a quicktip on what to put in squid.conf, so to make squid become a reverse proxy :

    ### the essentials
    http_port 80
    httpd_accel_host virtual
    httpd_accel_port 8181
    httpd_accel_uses_host_header on

    ### cache-related settings
    cache_mem 128 MB
    maximum_object_size_in_memory 1024 KB
    cache_dir ufs /usr/local/squid/var/cache 4096 16 256

    ### access control
    http_access allow all
    http_reply_access allow all
    icp_access allow all

  3. Appreciate your writing up this little guide.

    I already had squid running but it was version 2.5 and didn’t find out till after the upgrade to 2.6 that the syntax for the httpd_accel stuff had changed.

    Thanks to your page I didn’t have to comb through the docs for too long. 🙂

  4. iya ni imam boleh nanya………………
    sebelumnya saya ucapkan terima kasih banyak.
    gmn cara koneksi/setting jaringan sever pada client di linux tanpa hardisk,disini saya menggunakan igos relese 5?
    mohon di jelaskan dengan jelas.

  5. wordpress, php, and squid – they don’t work along well 🙂
    .
    Here’s an excerpt from a report made for one of my client :
    .

    WordPress & PHP, however, produces HTTP header which basically instructed Squid not to cache. We need to change this :

    ### editing /usr/local/lib/php.ini
    was: session.cache_limiter = nocache
    now: session.cache_limiter = public

    ### backed up /home/myblog/wp-includes/classes.php to /home/myblog/wp-includes/classes.php-original
    ### editing /home/myblog/wp-includes/classes.php, line 178
    ### WordPress version 2.3.1
    was: nocache_headers();
    now: //nocache_headers();

    .
    Now their website can handle hundreds of requests per second without breaking a sweat.

  6. i got it..installed squid and all but when I type my url it redirects me to apache test page instead of my site :/

  7. @john – I’m using my firewall to move the webserver port.
    .
    re: squid, looks like you have not enabled squid’s virtual switch.
    .
    If you’re using squid 2.5, the command is httpd_accel_host virtual
    .
    If you’re using squid 2.6 and later, the command is http_port 80 accel defaultsite=www.myaccelerateddomain.com vhost

  8. weird thing is I have that enabled.

    i changed my port back to 80 (changed it in httpd.conf) and the site is working but its probably not using squid

  9. dude..my server crashes everyday and im running a wordpress blog, do you think you can optimize it for me? how much do you charge? feel free to contact me in the email I provided here. thanks.

  10. Log squid bisa diset selengkap log apache detilnya? Misal untuk di-post-process pakai webalizer. Setelah dipasang squid sebagai reverse proxy kan semua entri log apache IP-nya berubah jadi IP si squid, jadi log apache relatif gak berguna setelahnya.

  11. @andika – bisa pak, ada settingan utk menentukan format log nya mau sedetil apa.
    .
    Tapi customer saya biasanya pada tidak peduli, mereka lebih pilih google analytics sejauh ini.

  12. ” Kya bate hai ” I mean what best you have done! cache_mem 512 MB, maximum_object_size 4096 KB, maximum_object_size_in_memory 1024 KB. This are all best.

    I like to use Squid 2.5. ITs really interesting……!
    EVEN I love my firewall to move the webserver port. just get such things and you to would feel great to have read this…..!

  13. you have done a great job here “to told us shift it from port 80″, it’s solves almost 60% of my problems with such large cache size & object size ,now i am going to install syqid & very-2 thankyou for this ‘huge new ngo + knowledge”

  14. Ayo dong dibuat bahasa kitanya Mas Harry, kalau saya susah deh (waktunya)… sekarang cukup penikmat saja…salam.

  15. Blu Ray Disc Copy is a professional and practical software which supports Blu ray movies copying as well as DVD copying. You can use this Blu Ray Disc Copy software to copy Bluray disc, Bluray folder, or Blueray ISO file without any quality loss. All the contents can be copied to the target file/disc quickly and perfectly. Blu Ray Disc Copy supports all Blu ray and DVD discs and it can remove AASC, CSS, and region protection from them. So that enjoying your Blu ray discs and DVDs freely and conveniently is no longer a problem for you.....

  16. It will do anything more than just that Don’t get me wrong I am all for alternative forms of energy but the problem their is right now most of them are not economically releasable.

  17. Wow!I found your blog via ws sign Google and I just wanted to say that I believe your writing is just stunning! Thanks again with regard to providing this content for free.

  18. It is clear increasing the consumption of products and/or services will not going to solve any problem, consumption as an aim by itself is not a solution but a problem.

  19. Great write-up, I’m normal visitor of one’s website, maintain up the excellent operate, and It’s going to be a regular visitor for a long time.

Leave a Reply

Your email address will not be published.