Blankego's Humble Home

Hosting multiple sites locally with nginx

Sometimes you wanna host multiple sites on your local station. It's not only good for testing , but also comes in handy if you want to organize your static pages (like documentations) in a clean and convenient manner.

In this post I'll set an example with ruby-doc, which you can acquire from http://ruby-doc.org , to show you how fun it is to make this kinda tricks.

If you're like me, dwell behind the Great frakking wall, you'll constantly be pissed by the capricious connection condition of sites abroad, the ruby-doc.org for example, sometimes good, sometimes bad, sometimes the pages simply won't load, no matter how viciously you curse. Lady Mengjiang could've brought down the Wall by crying, whereas your tears mean nothing Y_Y to those cold-hearted SOB. So we could work around by downloading the htmls and consulting it locally. Unfortunately the gzs provided by ruby-doc.org are in very bad shape. Links are broken, css/img/js are missing. The follow text will guide you to fix the docs to make them almost as readable and usable as the online version.

Setup a virtual host

sudo geany /etc/hosts 

Or you can use any of your favorite editors. Just open it, and add a line like that shown below at the end of the file:

#<ip-address>  <hostname.domain.org> <hostname>
127.0.0.1       localhost.localdomain   rdoc        #rdoc is the host name for your ruby-doc 

Configure nginx

First, make two directories:

sudo mkdir /etc/nginx/conf/sites-available
sudo mkdir /etc/nginx/conf/sites-enabled

For ubuntu these dirs should be created under /etc/nginx/.

Second, create a config file named after your virtual host (I'll use "rdoc" ) under sites-available dir.

server {
        listen       80;
        charset utf-8;    
        server_name rdoc;
        root /(path_to_my_rdoc)/;     
        #my path is "/media/f/Docs/ruby/rdoc/", don't you dare copy it;) 
        location / {      
                index index.html index.htm;
        }
}

Then add a line in /etc/nginx/nginx.conf (the main config file) to include your virtual host settings.

#...
http {
    include /etc/nginx/conf/sites-enabled/*;
#...

And don't forget to make a symbol link.

sudo ln -s /etc/nginx/conf/sites-available/rdoc  /etc/nginx/conf/site-enabled/rdoc

Create your local rdoc site

Down core-doc and stdlib-doc and extract the content to your chosen rdoc directory. Rename the directories ruby_1_9_2_core and ruby_1_9_2_stdlib to core and stdlib respectively. Then you download the js/css stuff package I stolen from ruby-doc.org, extract it to rdoc directory. Now your rdoc dir should have a structure like this:

rdoc
├── rdoc/core
│   ├── rdoc/core/css
│   ├── rdoc/core/doc
│   ├── rdoc/core/Encoding
│   ├── rdoc/core/Enumerator
│   ├── rdoc/core/File
│   ├── rdoc/core/GC
│   ├── rdoc/core/images
│   ├── rdoc/core/IO
│   ├── rdoc/core/js
│   ├── rdoc/core/Math
│   ├── rdoc/core/NameError
│   ├── rdoc/core/Process
│   └── rdoc/core/RubyVM
├── rdoc/css
│   └── rdoc/css/highlighter
├── rdoc/icons
├── rdoc/images
│   └── rdoc/images/books
├── rdoc/js
│   └── rdoc/js/libs
└── rdoc/stdlib
    └── rdoc/stdlib/libdoc

Finally, your just need to restart nginx:

sudo rc.d restart nginx

Fire up your browser, enjoy your shining new rdoc site!

blog comments powered by Disqus