Archive for the ‘Apache’ Category

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.

How to add Ruby (.rhtml) support to your existing MAMP setup with eRuby

Posted on August 18th, 2008 in Apache, ruby | No Comments »

Adding Ruby HTML support to your existing MAMP setup is great for experimenting and for building quick single serving apps where a whole Rails setup would be overkill. This is how I added Ruby RHTML page support to my current MAMP setup:

Download and compile eRuby

  1. Download and unzip http://www.modruby.net/en/index.rbx/eruby/download.html
  2. Go to the unzipped directory in terminal
  3. type ./configure.rb
  4. type make
  5. type make install
  6. Copy the generated eruby file to your local cgi-bin

Modify your httpd.conf settings

  1. add this line to add the rhtml support:
    AddType application/x-httpd-eruby .rhtml
    Action application/x-httpd-eruby /cgi-bin/eruby
  2. And modify this line so your server will look for index.rhtml pages in directories:
    DirectoryIndex index.html index.shtml index.rhtml

Running straight .rb files outside your cgi-bin folder (optional)

You can run any scripts from your cgi-bin if you properly add the #!(she bang) location to your ruby intepreter at the top of your scripts, but you can also make these scrips run outside of your cgi-bin by just adding this line to your httpd.config, just add the .rb extension to the filename:

AddHandler cgi-script .rb

I was doing this for awhile so I could execute scripts through the web but the RHTML method is better. Still, there could be applicable reasons for a person to want to do this. Just keep in mind, that when accessing Ruby scripts directly from a webpage you must at least specify a content-type (puts: “content-type: text/plain\n”) in your header or use the convenient cgi class from the Ruby standard library that does that (and a whole lot more) for you.

Improving Performance (optional)

The above is a really quick way to add Ruby support to your local webserver however, there’s one downside – it has to start up the Ruby interpreter everytime you hit an rhtml file with your browser. If you want something a little more dedicated look into the mod_ruby apache module which embeds a Ruby interpreter into Apache’s (inside your web server’s memory), to let Ruby CGI scripts execute natively, so scripts start up and run far faster. This is the equivalent as mod_php for PHP, that’s built into your MAMP setup.

Additionally, as a speed boost, you could use fast cgi to keep the eRuby interpreter instantiated, if for some reason you can’t use mod_ruby.

Helpful links:

http://www.rubycentral.com/book/web.html