Archive for August, 2009

Hunchentoot Virtual Hosts with nginx

August 5, 2009

(This posting is dated have a look at https://zaries.wordpress.com/2010/11/09/lisp-web-server-from-scratch-using-hunchentoot-and-nginx/ for a newer approach)

I looked at a couple of options to do virtual hosts with hunchentoot, but eventually I settled on nginx. It might not be the best solution but it does what i need with minimal effort. (I did not have to read and understand the source code to be able to use it or compile it or patch it or any of those nasty things that causes you to pull your hair out trying to remember or repeat it some other time.)

My setup is as follows. I have a vps server that is starting up sbcl in a screen session that loads one instance of hunchentoot. For me to be able to run multiple sites (same file names (NOT SAME FILES)) from the one instance of hunchentoot I used a prefix of the site name to differentiate individual sites. No its not greek.

Example:
http://localhost:8090/my-site/home.html
http://localhost:8090/my-other-site/home.html

(No I am not running huncnetoot on port 80 because that would cause me to pull out more hair.)

These then have to translate to domains like http://www.mysite.com (http://localhost:8090/my-site/) and http://www.myothersite.com (http://localhost:8090/my-other-site/).

I am not going to go into how to setup hunctentoot, there are plenty of tutorials on that subject, thou I would advise going the clbuild route.

Here is how I installed nginx on Ubuntu 9.04.

1. sudo apt-get nginx
(Yes I know it might not be the latest version but it will only serve as a reverse proxy and that part works fine.)

2. sudo gedit /etc/nginx/sites-available/default

I deleted all the stuff that was in there and replaced it with the following

server {
        #The port nginx must listen on
        listen   80;

        #The name of the domain to route
        server_name  www.mydomainname.co.za;

        #To default to the home page from  www.mydomainname.co.za;
        rewrite ^(.*)/$ $1/home.html;

        location / {
            #Where to route the listed domains too...in our case hunchnetoot
            proxy_pass http://localhost:8090/my-site/;

            #Some more config stuff if you want it. This can be moved to /etc/nginx/proxy.conf and 
            #the following line can replace everything up to proxy_buffers:
            # include /etc/nginx/proxy.conf;
            proxy_redirect          off;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   90;
            proxy_send_timeout      90;
            proxy_read_timeout      90;
            proxy_buffers           32 4k;
        }
       
        #This is needed to handle the links to all the images,stylesheets and javascript files 
        #This was also the bit that was missing from all the other examples I found. 
        location /mu-site/ {
                proxy_pass http://localhost:8090/my-site/;
        }
}

server {     
        listen   80;
        server_name  www.myotherdomain.co.za;

        rewrite ^(.*)/$ $1/home.html;
        location / {
            proxy_pass http://localhost:8090/my-other-site/;
        }
       
        location /my-other-site/ {
                proxy_pass http://localhost:8090/my-other-site/;
        }
}

Save and start nginx with sudo /etc/init.d/nginx start.

Now go to your favourate browser and browse for http://www.mysite.com or what ever you used as you domain.

PS: If anybody can give me an example of how to use hunchentoot-vhost to do the same I would really appreciate it.

Advertisements