Simon Zimmermann

2010-02-26

Nginxlamp

I’ve been stuck in ssh, apache conf files and /etc/init.d/apache2 reloads for the last couple of days. It’s a daunting task to get it right, and of course cheerfully rewarding when it finally works.

My plan was to create a testing environment for a web-site I’ll be working. The current site is located on a shared host so I wasn’t able to fully isolate and install a setup I would be comfortable with, since the production site and a few others where all on the same host aswell.

After an morning of banging my head against an old python interpreter on the shared host I signed up for a small VPS slice over at Linode.com’s London-server. It’s a small fee to pay to be freed of the restrictive nature of an shared host, totally worth it.

The dark side of the moon is obviously that you are stuck on your own. Thankfully Linode had a dozen guides to getting you started on the server config part. My plan was to have a Django-enabled server, including git and other helpful tools part of my development cycle. I wanted something flexible where I can test different ideas and setup’s which may be more suitable for my employer, then their current LAMP stack. Their current webpage is serving a 20GB lot of media-files through an apache server. And I’ve noticed the latency on their webpage when looking at static content. It looks to me as if they could win a lot by using a lighter setup as a front-end dispatcher(ehh, proxy), so I was keen on getting nginx up running as well.

I’m happy to say that I finally got a basic setup running. It’s mostly out-of-the-box configured, but I’ve tweaked it slightly for my Linode’s resources and played around with different strategies. Here’s a short list of the setup.

As an example I can post the nginx conf for the django-configured domain.
# Listen for <host name>
server {
    listen   <ip-address>:80;
    server_name <host name>;
    access_log /home/<directory>/logs/nginx_access.log;
    error_log /home/<directory>/logs/nginx_error.log;
    location / {
        proxy_pass  http://<ip-address>:8080;
        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_buffer_size           4k;
        proxy_buffers               4 32k;
        proxy_busy_buffers_size     64k;
        proxy_temp_file_write_size  64k;
    }
    location  /media/ {
        root /home/<directory>/media;
    }
}
Additionally this gives me the opportunity to say serve some requests directly with nginx and dynamic content with apache on the same domain. I could eventually do something like this; filter all static file request and serve them directly and send the dynamic content request by proxy to apache.
location ^~ /(images|css|js) {
        root   /home/www/www.domainname.org/media/;
        access_log  /var/log/nginx/d-a.direct.log ;
}

# Proxy all remaining content to Apache

location / { proxy_pass http://127.0.0.1:8080/; proxy_redirect off;

    ....

Getting my head around sys-admin things really is a learning full experience. Worth trying out.

robotics github