Fastcgi on nginx

About nginx

On paper, nginx is an excellent choice for use as a reverse proxy (together with wthttpd), or for deploying Wt applications using FastCGI, especially if you are using server-initiated updates (server push).

The reason is that nginx, like wthttpd, is completely based on asynchronous I/O, and therefore scales easily to many open connections (unlike other web servers such as apache, lighttpd, etc...). Server-initiated updates use an open connection for every web client.

We have tested nginx succesfully for both its use as a reverse proxy or for FastCGI deployment of Wt applications. Its FastCGI implementation is a bit clumsy, but newer versions of nginx (>= 0.7.31) than the version currently available in ubuntu should improve on that.

Ubuntu config

  • Installing nginx and spawn-fcgi (which comes with lighttpd):

<!-- -->

 $ sudo apt-get install nginx lighttpd
  • Start your application (linked against wtfcgi) using spawn-fcgi. Spawn-fcgi wraps the FastCGI application to listen to a socket or network connection.

<!-- -->

 $ spawn-fcgi -n -f ../../build/examples/hello/hello.wt -a -p 9091
  • Edit your nginx configuration to redirect a particular URL to your application (replace all occurences of /hello.wt with the path for your application!) :

<!-- -->

        location /hello.wt {

                fastcgi_param  QUERY_STRING       $query_string;
                fastcgi_param  REQUEST_METHOD     $request_method;
                fastcgi_param  CONTENT_TYPE       $content_type;
                fastcgi_param  CONTENT_LENGTH     $content_length;

                if ($document_uri ~ "^/hello.wt/(.*)") {
                        set $apache_path_info /$1;

                fastcgi_param  SCRIPT_NAME        /hello.wt;
                fastcgi_param  PATH_INFO          $apache_path_info;
                fastcgi_param  REQUEST_URI        $request_uri;
                fastcgi_param  DOCUMENT_URI       $document_uri;
                fastcgi_param  DOCUMENT_ROOT      $document_root;
                fastcgi_param  SERVER_PROTOCOL    $server_protocol;

                fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

                fastcgi_param  REMOTE_ADDR        $remote_addr;
                fastcgi_param  REMOTE_PORT        $remote_port;
                fastcgi_param  SERVER_ADDR        $server_addr;
                fastcgi_param  SERVER_PORT        $server_port;
                fastcgi_param  SERVER_NAME        $server_name;
  • Reload (or restart) your nginx server and you are ready to go:

$ sudo /etc/init.d/nginx restart

Updated by Roel Standaert over 6 years ago ยท 31 revisions