So I got to thinking. There are some good caching reverse proxies out there, maybe it’s time to check one out for beeets. Not that we get a ton of traffic or we really need one, but hey what if we get digged or something? Anyway, the setup now is not really what I call simple. HAproxy sits in front of NginX, which serves static content and sends PHP requests back to PHP-FPM. That’s three steps to load a fucking page. Most sites use apache + mod_php (one step)! But I like to tinker, and I like to see requests/second double when I’m running ab on beeets.

So, I’d like to try something like Varnish (sorry, Squid) but that’s adding one more step in between my requests and my content. Sure it would add a great speed boost, but it’s another layer of complexity. Plus it’s a whole nother service to ramp up on, which is fun but these days my time is limited. I did some research and found what I was looking for.

NginX has made me cream my pants every time I log onto the server since the day I installed it. It’s fast, stable, fast, and amazing. Wow, I love it. Now I read that NginX can cache FastCGI requests based on response caching headers. So I set it up, modified the beeets api to send back some Cache-Control junk, and voil√†…a %2800 speed boost on some of the more complicated functions in the API.

Here’s the config I used:

# in http {}
fastcgi_cache_path /srv/tmp/cache/fastcgi_cache levels=1:2
                           keys_zone=php:16m
                           inactive=5m max_size=500m;
# after our normal fastcgi_* stuff in server {}
fastcgi_cache php;
fastcgi_cache_key $request_uri$request_body;
fastcgi_cache_valid any 1s;
fastcgi_pass_header Set-Cookie;
fastcgi_buffers 64 4k;

So we’re giving it a 500mb cache. It says that any valid cache is saved for 1 second, but this gets overriden with the Cache-Control headers sent by PHP. I’m using $request_body in the cache key because in our API, the actual request is sent through like:

GET /events/tags/1 HTTP/1.1
Host: ...

{"page":1,"per_page":10}

The params are sent through the HTTP body even in a GET. Why? I spent a good amount of time trying to get the API to accept the params through the query string, but decided that adding $request_body to one line in an NginX config was easier that re-working the structure of the API. So far so good.

That’s FastCGI acting as a reverse proxy cache. Ideally in our setup, HAproxy would be replaced by a reverse proxy cache like Varnish, and NginX would just stupidly forward requests to PHP like it was earlier today…but I like HAproxy. Having a health-checking load-balancer on every web server affords some interesting failover opportunities.

Anyway, hope this helps someone. NginX can be a caching reverse proxy. Maybe not the best, but sometimes, just sometimes,  simple > faster.

Trackback

OMGOSH 5 comments

  1. It seems to be the architecture more and more people are converging to, and this will be especially true with keep-alive support in haproxy 1.4, because people won’t have to trade between availability and site load time for high latency users. Also nginx+haproxy work reliably and very fast together. Both can take advantage of each other’s optimisations to save CPU and network bandwidth. I’m aware of a very large site in the range of the million concurrent visitors making use of both. It would most likely not work if any of the two components were to be replaced.

  2. That makes sense. I haven’t looked at the keepalive stuff in HAProxy, I’ll check it out though. After doing some benchmarks with and without HAproxy, I’m convinced that having it slows down the request by about as much as adding a millimeter to the length of the network cable connecting the request to the machine. It’s almost like it isn’t there. Add the features and speed of NginX, plus the fact that it can do its own caching, and that’s a setup which for how insanely fast it is, really isn’t that complicated or high maintenance. Also really good for a lower-power server or VPS.

    Thanks for the comment!

  3. [...] while back I wrote a post about using NginX as a reverse-proxy cache for PHP (or whatever your backend is) and mentioned how I was using HAProxy to load balance. the main [...]

  4. [...] another bookmark Kommentare [...]

  5. The website should provide its visitors with efficient tools and features in order to make maximum use of what is provided in the website.
    SEO is such a popular term today that every blogger is striving to incorporate it
    into the content, meta tags and image tags. A good SEO article writing professional
    knows how to engage your readers at the same time, researchers using specific keywords can be used strategically
    placed in the article, so that the search engine will pick you up and places
    it in a better position when someone types in the keywords again.