# I recently switched one of my Rails applications from Litespeed to Apache+Passenger # with nginx on the frontend to handle static file requests. nginx proxies all non-static # file requests to Apache+Passenger. I use page caching extensively, so that is why I # need fast static file serving. # # I ran some quick tests comparing static file serving of Litespeed, Apache, and nginx. # Here are the results: # # nginx 4400 requests/second # Apache 2900 requests/second # Litespeed 5000 requests/second # (run with ab -n 100000 -c 20 http://www.mydomain.com/staticfile.html) # While nginx serves static files nearly as fast as Litespeed, nginx spikes the CPU to # about 50%. Litespeed hovers around 5%. # # So here is the configuration and built options that I am now using for nginx + Apache+Passenger # # BEGIN nginx configuration # Built with ./configure --with-http_ssl_module worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { #http listen 80; server_name www.YOURDOMAIN.com YOURDOMAIN.com; # redirect domain.com requests to www.domain.com if ($host !~* www\.(.*)) { rewrite ^(.*)$ http://www.$host$1 permanent; } root /var/www/apps/YOURAPP/current/public; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; if (!-f $request_filename) { proxy_pass http://localhost:8080; break; } if ($request_method != GET) { proxy_pass http://localhost:8080; break; } } } server { #https listen 443; server_name www.YOURDOMAIN.com YOURDOMAIN.com; if ($host !~* www\.(.*)) { rewrite ^(.*)$ https://www.$host$1 permanent; } ssl on; ssl_certificate www.YOURDOMAIN.com.crt; ssl_certificate_key www.YOURDOMAIN.com.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; root /var/www/apps/YOURAPP/current/public; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-FORWARDED_PROTO https; if (!-f $request_filename) { proxy_pass http://localhost:8080; break; } if ($request_method != GET) { proxy_pass http://localhost:8080; break; } } } } # END nginx configuration # BEGIN Apache 2.2 configuration # Built with ./configure --enable-ssl --enable-rewrite --enable-proxy --enable-proxy-http --with-mpm=worker User daemon Group daemon ServerRoot "/usr/local/apache2" ServerAdmin webmaster@yourdomain.com DocumentRoot "/usr/local/apache2/htdocs" Options FollowSymLinks AllowOverride None Order deny,allow Deny from all DirectoryIndex index.html Order allow,deny Deny from all Satisfy All ErrorLog "logs/error_log" LogLevel warn LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common # You need to enable mod_logio.c to use %I and %O LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio CustomLog "logs/access_log" common DefaultType text/plain TypesConfig conf/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz SSLRandomSeed startup builtin SSLRandomSeed connect builtin Listen 127.0.0.1:8080 NameVirtualHost 127.0.0.1:8080 RewriteEngine On #RewriteLog "/usr/local/apache2/logs/rewrite.log" #RewriteLogLevel 10 LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.6 PassengerRuby /usr/local/bin/ruby ServerAdmin webmaster@YOURDOMAIN.com DocumentRoot "/var/www/apps/YOURAPP/current/public" ServerName YOURDOMAIN.com ServerAlias www.YOURDOMAIN.com ErrorLog "logs/YOURDOMAIN.com-error_log" CustomLog "logs/YOURDOMAIN.com-access_log" common Options FollowSymLinks AllowOverride None Order allow,deny Allow from all # END Apache configuration