Web Application on Erlang: Configure Nginx with Mochiweb

My previous post, the first of our series dedicated to the Erlang world, was definitively getting too long, so here is the second chapter of this marvelous article.

I'll assume that you have successfully installed Erlang and Mochiweb and deployed your first small tiny app.

When it comes to deliver static content at lightning speed and do reverse proxy job, there is one name : Nginx, the Igor Sysoev high-performance HTTP server.

It plays well with ssl and in the last dev version you can even find an interesting module for uploading files, nothing less ... we will probably tell you more about that in the series.

Nginx is really turning itself into a real handy swiss knife.

Step 0 - SSL packages check

So let's start by ensuring that the ssl packages are already installed on your box :

foo@bar:~$ sudo apt-get install openssl libssl-dev

Step 1 - Install Nginx

Now, we are ready to download, compile and install Nginx.

Here, we want to install the last stable version (0.6.32).

foo@bar:~$ sudo apt-get build-dep nginx
foo@bar:~$ wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
foo@bar:~$ tar -zxvf nginx-0.6.32.tar.gz
foo@bar:~$ cd nginx-0.6.32
foo@bar:~/nginx-0.6.32$ ./configure --with-http_ssl_module
foo@bar:~/nginx-0.6.32$ make && sudo make install

Done.

Step 2 - Nginx configuration

We do have now to tell Nginx what to proxy, in our case http://my_server_name:8000 (in the previous post, remember?)

You can find the Nginx configuration file, nginx.conf, under /usr/local/nginx/conf/.

Here is a working example one :

Discover the code in Friendpaste

user www-data;

worker_processes  4;
...

http {
    include       mime.types;
    ...

    upstream mochiweb {
        server my_server_name:8000;
    }

    server {
        listen       80;
        server_name  my_server_name;

        ...

        location / {
            root   path_to_myapp/myapp/priv/www;
            index  index.html index.htm;
            ...

            if (!-f $request_filename) {
                proxy_pass http://mochiweb;
                break;
            }

        }

    }

}

Step 3 - Start, Stop Nginx

Finally, we are going to tell Ubuntu how to start and stop the server.

Just create a new file named 'nginx' in /etc/init.d and paste the content of this link:

Discover the code in Friendpaste

foo@bar:~/nginx-0.6.32$ cd /etc/init.d
foo@bar:~/etc/init.d$ sudo vi nginx

Above, for those of you who are not in love with vi, you can use nano instead: sudo nano nginx

foo@bar:~/etc/init.d$ sudo chmod +x nginx
foo@bar:~/etc/init.d$ sudo update-rc.d nginx defaults
foo@bar:~/etc/init.d$ sudo ./nginx start

Cool, you have now a working reverse proxy.

foo@bar:~/etc/init.d$ cd ~/myapp
foo@bar:~/myapp$ ./start-dev.sh

Browse this link http://my_server_name

Peace!

As a reminder, the next points we will discuss are:

  • The basic configuration of Postfix (mail)
  • The use of Imagemagick to create dynamically a captcha for your application
  • The configuration of Bind9 in order to play with the url CNAME

So, stay tuned

Read more

Comments

  1. [...] the next post, I’ll finish this basic install with the configuration of Nginx and I’ll provide you with a small JSON app [...]

  2. Pavel says:

    Thank you for your posts. I am waiting impatiently for a next one.

  3. Mark says:

    Can you please explain the purpose of nginx with mochiweb?
    I understand that nginx can be used as a load balancer in front of cluster of mochiweb machines but is there another implementation and advantage of using nginx and mochiweb on the same machine?
    Can nginx handle all the http overhead and hand mochiweb only the data, and then get back the data wrap it with http headers and send it back?

    Thanks

  4. Hughes says:

    @Mark

    Nginx and Mochiweb don't have to run on the same machine, but often do in a dev environment because of the small Nginx footprint in memory. Both play well together. Nginx is a really good web server for delivering static files. It proposes out of the box https support, modules highly optimized (i.e. upload) and some other features(gzip, ...) that are not provided by Mochiweb, the application web server.

  5. Eric says:

    Linux noob here. Sorry if I'm missing something obvious.

    I'm getting an error on step 1 when running: "make && sudo make install"

    Any thoughts?

    make -f objs/Makefile
    make[1]: Entering directory `/home/foo/nginx-0.6.32'
    gcc -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wno-unused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
    -o objs/src/core/nginx.o \
    src/core/nginx.c
    cc1: warnings being treated as errors
    src/core/nginx.c: In function ‘main’:
    src/core/nginx.c:244: error: ignoring return value of ‘write’, declared with attribute warn_unused_result
    src/core/nginx.c:249: error: ignoring return value of ‘write’, declared with attribute warn_unused_result
    src/core/nginx.c:257: error: ignoring return value of ‘write’, declared with attribute warn_unused_result
    make[1]: *** [objs/src/core/nginx.o] Error 1
    make[1]: Leaving directory `/home/foo/nginx-0.6.32'
    make: *** [build] Error 2

  6. Hello! Congratulations on your important contribution. Thank you! Looks good to me. I only use email for my business communications. I favor Outlook as my e-mail client and with the help of Email Sorter Wizard, an Outlook add-on, I get all my e-mail managed. I am sure people will benefit from your blog.

Leave a Reply