Project

General

Profile

Fastcgi on nginx » History » Revision 15

Revision 14 (Wim Dumon, 05/18/2011 11:43 PM) → Revision 15/31 (Andy Z, 10/21/2013 07:23 PM)

h1. FastCGI on nginx 

 {{toc}} 


 h3. 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. 


 h3. Ubuntu configuration 

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

 <pre> 
  $ sudo apt-get install nginx lighttpd 
 </pre> 

 * Start your application (linked against wtfcgi) using spawn-fcgi. Spawn-fcgi wraps the FastCGI application to listen to a socket or network connection. 

 <pre> 
  $ spawn-fcgi -n -f ../../build/examples/hello/hello.wt -a 0.0.0.0 -p 9091 
 </pre> 

 * Edit your nginx configuration to redirect a particular URL to your application (replace all occurences of /hello.wt with the path for your application!): 

 <pre> 
         location /hello.wt { 
                 fastcgi_pass     127.0.0.1:9091; 
 
                 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; 
         } 
 </pre> 

 * Alternatively to avoid 'if' in declaration just indicate the root of your application: 
 <pre> 
         location ~ ^/hello.wt(?<path_info>/.*|$) { 
                 fastcgi_pass     127.0.0.1:9000; 
                 fastcgi_param    SCRIPT_NAME /hello.wt; 
                 fastcgi_param    PATH_INFO $path_info; 
                 include          fastcgi_params.conf; 
         } 
 </pre> 

 * Reload (or restart) your nginx server and you are ready to go: 

  $ sudo /etc/init.d/nginx restart