Archive for the ‘Apache’ Category

Nginx and Apache rewrite to support HTML5 Pushstate

Posted on May 17th, 2012 in Apache, backbone, nginx | No Comments »

HTML5 pushstate is awesome. It enables you to change the URL of your site dynamically without refreshing the page (goodbye hashes!). Libraries like Backbone have great support for this. Unfortunately if a user bookmarks or refresh a page on an app that’s using HTML5 pushstate, it makes a request to the server for that deep linked content. Here are the rewrites for Nginx and Apache to internally redirect that call to the same html file. Browser thinks its a unique page but it’s the same.

Apache

In your vhost :

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

Nginx

    rewrite ^(.+)$ /index.html last;

Upgrade Centos LAMP stack to PHP 5.4

Posted on April 4th, 2012 in Apache, Linux | No Comments »

The new PHP 5.4 is out, and it’s really awesome (for being PHP). Among it’s many new features is a new shortened array syntax similar to Ruby, Python, and JavaScript. I remember when I was into PHP a few years ago this was the biggest request from developers and PHP core-team was vehemently against it. It seems they have given in and now the code looks way nicer and fun to write:

foreach (['common', 'model'] as $class_path) {
    spl_autoload_register(function($class) use ($class_path, $doc_root) {
        $class_path = "$doc_root/classes/$class_path/$class.class.php";
        if (file_exists($class_path))
            require_once($class_path);
    });
}

To upgrade your Centos LAMP stack compile PHP 5.4 like so:

cd /tmp
wget http://www.php.net/get/php-5.4.0.tar.gz/from/a/mirror
tar xzf php-5.4.0.tar.gz
cd php-5.4.0*
./configure --with-apxs2=/usr/sbin/apxs --with-mysq --with-mcrypt --without-iconv # --with-mcrypt=/opt/local for OS X
make
make test
sudo make install

Mcrypt param is optional if you don’t have it and need it.

sudo yum | apt-get | port install libmcrypt # libmcrypt-devel for centos

Nginx, Apache, and Node all living harmony.

Posted on July 15th, 2011 in Apache, nginx, node | No Comments »

So here’s two problems you want to solve:

You want to optimize static content

You have an Apache install that’s hosting a bunch of sites and friend’s sites through vhosts. One of your blogs is getting a lot of hits and you want to optimize it’s static content – or even the static content of all sites. You’re not quite ready for a CDN-type deal, just to place the content outside of Apache and into something more lighter-weight so it’s not running through WordPress / Apache and unnecessarily using up threads (at roughly 2mb a thread).

You want all your web apps on port 80!

You started really getting into Node (or Ruby on Rails or Django) but every web app needs to be binded to it’s own port and port 80 is taken by your Apache which is hosting a lot. You don’t want to be giving out the url: http://mycoolnewapp.com:81.

Sure you can use Apache’s proxypass but you’re gaining overhead and, in case of Node (or Ruby’s EventMachine or Python’s Twisted), you’re losing the whole point of having an optimized, non-blocking / non-threaded, web app.

The solution

Nginx! Nginx is a web server, reverse-proxy, load balancer, and mail server all in one. Like Node, it was built with the concept of the event loop, not threads so it’s highly optimized for high concurrency. The idea is to setup Nginx to be a reverse-proxy for all your other services.

I’ll skip over how to install Nginx as it’s pretty straightforward and you can google it. I’ll go over the main steps to getting Apache to work through Nginx – it’s truly easy, I did it on my first try. The only problem that I encountered is that since the user only interfaces with Nginx – it’s Nginx that is making the requests to Apache / Node. So from Apache’s perspective, all requests are coming from 127.0.0.1. We also fix this in the steps below.

  1. Once Nginx is installed, edit your /etc/httpd/conf/httpd.conf so that it listens on another port. Say 127.0.0.1:8080.
  2. Switch all your vhosts to also listen on this port.
  3. Edit your /etc/nginx/nginx.conf file. Make sure it’s set to listen on port 80 and add a server entry per each apache vhost. Here’s an example:
        server {
               listen       80;
               server_name  mysite.com;
    
    	   location / {
                        proxy_pass http://127.0.0.1:8080;
                        include /etc/nginx/conf.d/proxy.conf;
               }
        }
    
  4. Next we’ll add that proxy.conf reference by creating /etc/nginx/conf.d/proxy.conf:
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 8m;
    client_body_buffer_size 256k;
    proxy_connect_timeout 60;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    proxy_buffer_size 4k;
    proxy_buffers 32 256k;
    proxy_busy_buffers_size 512k;
    proxy_temp_file_write_size 256k;
    
  5. At his point you need to install mod-rpaf for Apache. This enable Apache to use the extra headers Nginx is passing in the request. If you’re using a flavor of Linux that uses apt-get you’re in luck. Just run: sudo apt-get install libapache2-mod-rpaf. If you’re using a system that uses Yum, you’ll have to compile it yourself. Just follow the steps here.

As for serving static content. Inside each “server” declaration you can add the following (modified to your taste):

       location ~* ^.+\.(jpg|jpeg|gif|png|ico|tgz|gz|pdf|rar|bz2|exe|ppt|txt|tar|mid|midi|wav|bmp|rtf) {
            root /folder/to/static/files;
            expires 90d;
       }
       location ~* ^.+\.(css|js)$ {
            root /folder/to/static/files;
            expires 30d;
      }



Ad that’s it you’re done!

Additional reading

Nginx Primer
Nginx Primer 2: From Apache to Nginx
Apache with Nginx
Really solid config samples

Expires header doesn’t work on the iPhone / iPad

Posted on December 17th, 2010 in Apache, iOS, iPhone | No Comments »

I’m optimizing the web views of an iPhone app in preparation for Christmas traffic and one of the ideas I had was to use small increments of Expires header to cache files client-side. I assumed this would be no problem as all my work with Safari on the iPhone has shown it a very capable browser—as good as any desktop one.

Turns out after some testing that using Expires Header isn’t possible— not because of cache size limitations as one would assume. It looks like the guys in Cupertino wanted to limit this ability on the iOS as every HTTP request made by an iOS device issues a pragma: no-cache directive. I’ve yet to understand why. Better fire up more memcache servers.

Here’s the raw headers if interested:

GET /expires/ HTTP/1.1
Host: readystate4.com
User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Cookie: __utma=244034822.428964265.1285289496.1290546798.1290723961.13; __utmz=244034822.1285289496.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=1.1229958817.1285277083.1288920090.1292540519.14; __utmc=1; __utmz=1.1285277083.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Pragma: no-cache
Connection: keep-alive

Always remember: you can’t “expires header” an HTTP POST

Posted on December 17th, 2010 in Apache | 1 Comment »

This includes ajax content (which to the server is just a standard HTTP request.) This makes sense since POST should only be used for storing or updating data.

Thanks to this page.