SEObox: Web Hosting Murah Unlimited Homeschooling Indonesia

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.

19 Responses to “Building Edge Server With Squid 2.6

  • 1
    dikshie
    May 19th, 2007 22:36

    utk *reverse proxy/http accelerator* cobain pakai varnish:
    http://varnish.projects.linpro.no/
    *katanya* performance nya diklaim jauh lebih baik drpd squid.

  • 2
    andriansah
    May 20th, 2007 07:04

    thank for the info

  • 3
    Proxy Templates » Blog Archive » Proxy Servers May 19, 2007 11:05 am
    May 21st, 2007 03:32

    [...] Building Edge Server With Squid 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, … [...]

  • 4
    sufehmi
    June 12th, 2007 01:46

    @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.

  • 5
    sufehmi
    June 12th, 2007 01:49

    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

  • 6
    dikshie
    June 12th, 2007 07:46

    http://www.des.no/pub/2007-06-08%20Varnish%20eZ.pdf
    http://phk.freebsd.dk/pubs/varnish_roadshow.pdf

  • 7
    Jon Davis
    June 29th, 2007 11:16

    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. :)

  • 8
    sufehmi
    June 29th, 2007 14:17

    @Jon - you’re welcome. Thanks for letting me know that you find it useful.

  • 9
    imam
    July 28th, 2007 18:53

    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.

  • 10
    sufehmi
    December 3rd, 2007 17:29

    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.

  • 11
    john
    January 23rd, 2008 06:02

    how do you move the webserver port? thanks!

  • 12
    john
    January 23rd, 2008 08:43

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

  • 13
    sufehmi
    January 23rd, 2008 09:39

    @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

  • 14
    john
    January 23rd, 2008 09:46

    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

  • 15
    john
    January 23rd, 2008 21:49

    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.

  • 16
    Andika Triwidada
    March 11th, 2008 00:32

    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.

  • 17
    sufehmi
    March 11th, 2008 02:56

    @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.

  • 18
    piju
    June 12th, 2008 18:17

    performance squid di freebsd nampaknya lebih hebat

  • 19
    BandwidthTest
    September 3rd, 2008 13:49

    ” 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…..!

Leave a Reply

Subscribe without commenting