Using Nginx’s Embedded Perl Module

dp, 14 April 2010, 1 comment
Categories: Servers
Tags: , , ,

At work, we have Nginx sitting in front of Apache, to take some of the load off. We’ve also decided that we should use our bandwidth sparingly, and compress/minify Javascript/CSS. To do that, we’re using the built in Embedded Perl Module functionality inherent within Nginx. But because we have several different sites running on a single Nginx server, we need to make sure that we store the .js/.css files in the correct location, or run the risk of overwriting the wrong files or even serving incorrect data.

The documentation suggests that any request header can be pulled via $r->header_in(). However, it doesn’t give you a list of header conversions. Normally, you’d use SERVER_NAME, or the like. But in Nginx, it’s called host. Unfortunately, that information doesn’t seem to be disseminated very widely, so I’m posting it here, in the hopes that it will help some other lost traveler later.

Comments

  • Michael

    I’m thinking I might need to use the embedded Perl functionality, though I’m wondering about the extent of its capabilities and how fast it is.  I want to use nginx to serve media (images, etc.); however, I need to authorize that the client can view said media.  The main app is written in Java and all the authorization logic lies there. So I’m thinking I’ll serve media with nginx and have a Perl module make a subrequest to my Java servlet, handing it the request details.  If the servlet responds that the client can view the media file, then I’ll have Perl tell nginx to go ahead and send the file.  I saw some examples of Perl modules calling sendfile(…), but I’d prefer nginx to do all the static file handling since it’d probably be faster and wouldn’t take up nearly as much memory.  Any way to have Perl set a variable in nginx and then have nginx send the file to the client if the variable has a certain value?  Something like: if (perl module says ok) { serve file; } else { send http 403 forbidden; }