Categories
Uncategorized

How We Did It: Millions of Daily Pageviews, 60% Less Server Resources

How We Did It: Millions of Daily Pageviews, 60% Less Server Resources






Contributed to by Trevor Suarez. Two years ago at One Mighty Roar we noticed that a side-project from the early days of the company was gaining large amounts of traffic, despite not being touched in ages. Over the process of a few months, we spent some 20% time, which quickly turned into 120% time, revamping You Rather, redoing the site from the ground up, creating an API, and writing both an iOS and an Android app. As a result, You Rather has done some excellent numbers, gained some community garnishment, and been a great project for the OMR team to boast about. But, as most side-projects are, they fall low on the priority list when other new opportunities come along. At the end of this summer, it became our goal to give You Rather a breath of new life. The first step was to axe the aged Apache HTTP server in favor of Nginx. We’ve been using Nginx for 99% of our work over the last year and haven’t looked back since. However, most of our Nginx experience has been writing .conf files for new sites and servers, never rewriting old .confs for existing production sites. In just an afternoon, we moved a site with 400+ active concurrent users doing 1k+ pageviews a second, from Apache to Nginx, without any downtime. Brushing off the Dust To give some background, we ♥ AWS. You Rather uses every bit of the AWS stack, from EC2 to EBS to Route 53 to S3. To get a “dev” environment setup for ourselves, we grabbed our current AMI of the You Rather site and spun up a new instance for us to hack on. A simple yum install nginx got us Nginx up and running on our CentOS box in no time. Step one, complete. To start, we tossed up our generic Nginx conf: server { # Port declaration listen 80 default_server; listen 443 ssl; # Server name and aliases server_name nginx-test.yourather.com; # Root directory and default index root /var/www/path/of/yourather/src/; # Catch-all location / { try_files $uri $uri/ /index.php?$args; } # Pass the PHP scripts to the PHP-FPM socket location ~ .php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # PHP-FPM (unix socket) fastcgi_pass unix:/tmp/php5-fpm.sock; } } Lo and behold, most things…just worked. Granted, we had done plenty of work getting the AMI setup with with Apache and PHP initially, switching over to Nginx was pretty easy to get started. Step two, complete. Tweaking for Performance Nginx has a few obvious benefits over Apache when it comes to the server layer, but Nginx isn’t the sole reason for You Rather’s performance improvement. To see why, let’s clarify what exactly makes the difference here. Where Nginx really shines is that it doesn’t process “.htaccess” files. While those files make for convenient setups on shared hosting plans or shared machines, traversing the file directory for those files occurs on each request, which gets expensive. Nginx, on the other hand, loads all configs in at launch and that’s it, you’re good to go. Another place we saw had room for improvement was the interaction between our webserver and PHP. Our current implementation of You Rather used mod_php with Apache. Although the initial setup for Apache and mod_php was quick and easy, a big disadvantage to this is the way PHP is processed…


Read the rest of the post over at: How We Did It: Millions of Daily Pageviews, 60% Less Server Resources“>build the internet