Archive | English RSS feed for this section

Return a file with Nginx based on its freshness

21 Aug

In previous posts we talked about Nginx, that excellent lightweight web server made in Russia. Nginx is powering the Visualizeus servers since day one.

The other day I was needing some feature on Nginx. In Visualizeus there’s a file cache for some things, like the feeds and so on. Till now if the requested url has a cached version, then Nginx returned it with no more questions. If not, then Nginx transfer the request to the backend, which generates a new cached version in disk for the following requests for that resource. Pretty normal.

But Nginx wasn’t aware of the freshness of the file at all, it only understands that easy logic: file exists, then return it. So to avoid stale cache it’s obvious we need to delete from time to time that cached resources. When to delete it depends on the type of object we’re caching, of course. For example, the feeds are deleted each time a new image appears in that feed, and so on. But it was a little bit cumbersome for some objects, like the image pages, where you don’t have a clear condition that makes the page stale. It needs some cron tasks to run periodically and find files older than X, and those tasks eats a lot of resources specially when we’re talking of thousands and thousands of files and folders. So it was clear a better solution was needed here.
[...]

Parsing tables with XPath without hardcoded indexes

24 Sep

Let’s say you have to get some data from a XML provided by a 3rd party. You don’t have access or rights to modify that XML and the semantic, well, is not as good as you, semantic lover, would like to. In fact, let’s face it, the XML sucks in semantic and it has tables inside that makes you remember the nineties and those HTMLs.

First thing you do is a XPath expression based on hardcoded indexes, of course. “Please sir, would you mind to give me the TR[4] from TD[1]? Yes, I already know where is this data from, thanks”. But let’s imagine for a moment that your friend, the 3rd party, decide one day to change the XML, oor simply to change the order of the fields in the table, oor to add more fields to the table, oor… You don’t want to be dependent on the 3rd party mood to change your code and your XPath expressions one day and another, right? So better live without hardcoded indexes, if you can.

But… how? I had to fight with two scenarios, and I managed to get it done without hardcoded indexes. Of course your mileage may vary, but here’s how I did it:
[...]

Just a thought

23 Sep

What about headphones plug with MagSafe on it?

I’m not sure if they can managed to do the MagSafe small enough for a jack 3.5 connector, but my iPod Nano and my headphones will indeed appreciate it so much ;D

Updating Nginx to the 0.6.x branch

23 Jun

As I’m right now moving Visualizeus to a new server, this time a dedicated one because of the huge growth, I saw that the branch 0.6.31 was the latest stable Nginx version. So, taking advantage of the server move, I decided also to update the Nginx version.

So if you don’t want to lose your time as I did, specially when moving to a new server where you can’t find where the bug is coming so easily, please notice the following:

Changes with nginx 0.6.7 15 Aug 2007

*) Change: now the paths specified in the “include”,
“auth_basic_user_file”, “perl_modules”, “ssl_certificate”,
“ssl_certificate_key”, and “ssl_client_certificate” directives are
relative to directory of nginx configuration file nginx.conf
, but
not to nginx prefix directory.

Stupid little thing but not documented in the official Nginx wiki (till now ;))

ps: Whoah! I didn’t know that WordPress.com and even YouPorn were powered by nginx! Go, huge little russian server, go! :D

True Story

4 Jun

I crack up laughing with this one I’ve found on Visualizeus:

Web Design Timeline

Installing Munin plugins for Memcached and Nginx

21 Apr

I probably wasn’t very sharp when I installed these plugins for Munin, but the thing is I stumbled upon several small complications and so few information about them, so I’m posting this in case anyone can find it useful.

Munin plugins

Installing Munin plugins is easy, and there’s plenty of information around. The only thing you have to keep in mind is if the plugin name end in underscore, for example memcached_, means that when you create the symbolic links in /etc/munin.d/plugins you have to put after the underscore the parameter to monitorize. Best way is take a look into the plugin source code to see what parameters are supported.

Nginx plugin for Munin

You can get it at muninexchange, very interesting site for the munin freaks by the way, in particular here. There’re two, nginx_request and nginx_status.
[...]

Updating WordPress with Subversion

18 Apr

As I’m in a daze with these things, I haven’t realized that the “so expected” 2.5 version of WordPress was out. And I have strong reasons to suspect ;)

The thing is today I stumble on wp-scanner, a security scanner for WordPress (which I’m not pretty sure if it works well or just works) and well, it makes me realize the oldest version of WordPress was beer2beer with the 2.1.2 -ahem-

Ok, ok, I know. It’s a little bit, ehem, outdated… But let’s face it, updating WordPress  is  was a real pain in the ass. And having new releases almost each week doesn’t help neither.

And here’s where I recall the golden rule (one of them): make people’s life easier, not harder. It’s already hard enough for you to make it harder.

So, let’s get to the point: How to upgrade WordPress painless? See what I discovered today:

Installing/Upgrading WordPress with Subversion

Awesome! Almost as easy as an apt-get upgrade! I’ve just found some problems with some plugins in the Fregocles blog, which actually uses a lot of plugins, but for the rest, pretty straightforward!

Enjoy!

Automatically version your Javascript files with Subversion

17 Feb

We’ve already talked about serving static files in a good way. This post can be read as a follow-up to the Particletree article “Automatically Version Your CSS and JavaScript Files“.

A little excerpt of the article (read it!): When serving static files like .js or .css, the idea is having them cached by the client (a.k.a. browser) for a very long time (let’s say 10 years). But if we modify one of this static files, we need a way for the client to get the new version downloaded again. With that in mind we introduce the “version” of the file in the name, either with mod_rewrite (or similar), either in the querystring as a parameter. Changing the version number will get the browser think is a new file, and get it downloaded again.

But as said, read the Particletree article, is a lot better explained there.

Version

Now the thing is how to determine that version number for each static file. Obviously the idea is not having it done by hand, and that’s why in the Particletree article they use the filemtime() function, to get the last modification timestamp for the file.

Although it’s a first option, personally I don’t like it so much. That would imply a disk access each time an uncached static file is requested, and even we can avoid that disk access saving the timestamps in a include file… Why not use the repository version itself?

The staticVersions.py script

And here’s my humbly contribution. Is a Python script that scans the specified folders for static files (js and css), and returns a PHP array with the name of the files and its version in the Subversion repository, that is, when the last change happened.
[...]

Loading an external Javascript from a bookmarklet (Safari)

7 Oct

Loading an external javascript file from a bookmarklet is a good way to give superpowers to your favelet, and achieve what we really want, without care about the stupid 512 characters limit Internet Explorer has or without write all the code as an ugly one liner. And not to mention other advantages like server compression (i.e. js chunked and minified, gzip, …), PHP pre-processing and so on, that makes this technique in a very powerful resource when developing bookmarklets.

This post is because I recently discovered that Safari browser is a little picky when using this technique.

For example, what in other browsers (Firefox, Opera and IE) works like a charm, in Safari does not:

Note: Examples are really one liners but presented multiline for better reading.


<a href="javascript:void(
        z = document.body.appendChild(document.createElement('script'));
    );
    void( z.language = 'javascript' );
    void( z.type = 'text/javascript' );
    void( z.src = 'http://blabla.com/test.js' );
    void( z.id = 'testScript' );
">

To make Safari happy we have to polish it a little bit:
[...]

Having fun with Python

23 Jul

I was doing some lightweight scripting like this:

pattern = re.compile("searchingFor", re.IGNORECASE)

tables = [];
for line in fp.readlines():
    if pattern.search(line):
        tables += line
return tables

And then I remembered how fun Python could be with things like the list comprehensions:

pattern = re.compile("searchingFor", re.IGNORECASE)
return [ line for line in fp.readlines()
                if pattern.search(line) ]

Nice, huh? If you want to find out more about mapping lists, keep reading here.

Update: Even better! Cleanear, intuitive, two liner… hell yeah!

pattern = re.compile("searchingFor", re.IGNORECASE)
return filter( pattern.search, fp.readlines() )

Reticulator

12 Apr

Rescued from the total darkness of my hard disk, a little experiment in Python I made a couple of years ago…

The Reticulator!

Basically is a web tool to make modular grids in CSS. It has some minor bugs but as said, it was a little experiment, so we’ll have to live with them. The CSS generated is mostly cross-browsing (at least, it was :D) and the grid positions are based on position:relative not position:absolute.

Source code is availablehere (it works as a command line tool also).

WordPress and HTTP Basic-Auth

9 Apr

This post is for those of you with the same problem, say thanks to Google!

Problem: With WordPress and permalinks enabled, if you protect a folder or a file with .htpasswd, you’re going to get a nasty 404 Not Found.

Solution: Just add to your .htaccess file the following lines:

ErrorDocument 401 /[path_to_the_file]/myerror.html
ErrorDocument 403 /[path_to_the_file]/myerror.html

When /[path_to_the_file]/myerror.html is an existant static file in the server.

Explanation: Seems like the HTTP Basic-Auth returns a 401 Unauthorized to ask for the password. This got translated into a ErrorDocument directive, and if you haven’t properly set it up in your .htaccess (for example, pointing to some non-existant file), then it will be finally got by WordPress, which will return a 404 error.

Conclusion: Madness is here! I don’t understand how this isn’t better documented. Hope this helps!