<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nuno Neves</title>
	<atom:link href="http://www.nunoneves.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nunoneves.com</link>
	<description>O meu bloco de notas!</description>
	<lastBuildDate>Sun, 05 Feb 2012 20:33:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Minificar CSS e Javascript com o nginx</title>
		<link>http://www.nunoneves.com/2012/01/minificar-css-e-javascript-com-o-nginx/</link>
		<comments>http://www.nunoneves.com/2012/01/minificar-css-e-javascript-com-o-nginx/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 16:37:45 +0000</pubDate>
		<dc:creator>nuno</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.nunoneves.com/?p=1</guid>
		<description><![CDATA[O nginx é um servidor web fantástico com possibilidades de expansão por módulos praticamente ilimitado. Utilizando o módulo de perl embebido é possível minificar css e javascript de forma automática. O exemplo que se segue foi adaptado de vários exemplos &#8230; <a href="http://www.nunoneves.com/2012/01/minificar-css-e-javascript-com-o-nginx/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>O nginx é um servidor web fantástico com possibilidades de expansão por módulos praticamente ilimitado.<br />
Utilizando o módulo de perl embebido é possível minificar css e javascript de forma automática.<br />
O exemplo que se segue foi adaptado de vários exemplos que encontrei por forma a conseguir a configuração desejada.<br />
De salientar que não sou programador e perl para mim é como pilotar um avião <img src='http://www.nunoneves.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Para o exemplo utilizo o ubuntu server 10.4 lts:</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> add-apt-repository ppa:nginx<span style="color: #000000; font-weight: bold;">/</span>stable<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> update <br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libcss-minifier-xs-perl libjavascript-minifier-xs-perl nginx-extras</div></div>
<p>De seguida grave o ficheiro em baixo como /etc/nginx/perl/Minify.pm e altere as permissões para execução (chmod 755)</p>
<div class="codecolorer-container perl railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Minify<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> nginx<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> JavaScript<span style="color: #339933;">::</span><span style="color: #006600;">Minifier</span><span style="color: #339933;">::</span><span style="color: #006600;">XS</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> CSS<span style="color: #339933;">::</span><span style="color: #006600;">Minifier</span><span style="color: #339933;">::</span><span style="color: #006600;">XS</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">sub</span> css_handler <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cache_dir</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/tmp&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;.min.css&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=~</span><a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a><span style="color: #339933;">!/!</span>_<span style="color: #339933;">!</span>g<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cache_dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uri</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$filename</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">filename</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$/</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/undef.html"><span style="color: #000066;">undef</span></a><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> DECLINED <span style="color: #b1b100;">unless</span> <span style="color: #339933;">-</span>f <span style="color: #0000ff;">$filename</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>f <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>INFILE<span style="color: #339933;">,</span> <span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Error reading file: $!&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$css</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;INFILE&gt;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>INFILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>OUTFILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">'&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Error writing file: $!&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> OUTFILE CSS<span style="color: #339933;">::</span><span style="color: #006600;">Minifier</span><span style="color: #339933;">::</span><span style="color: #006600;">XS</span><span style="color: #339933;">::</span><span style="color: #006600;">minify</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$css</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>OUTFILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">send_http_header</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'text/css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">sendfile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> OK<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;<br />
<span style="color: #000000; font-weight: bold;">sub</span> js_handler <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cache_dir</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/tmp&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;.min.js&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=~</span><a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a><span style="color: #339933;">!/!</span>_<span style="color: #339933;">!</span>g<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$cache_file</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cache_dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uri</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$filename</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">filename</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/local.html"><span style="color: #000066;">local</span></a> <span style="color: #0000ff;">$/</span><span style="color: #339933;">=</span><a href="http://perldoc.perl.org/functions/undef.html"><span style="color: #000066;">undef</span></a><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> DECLINED <span style="color: #b1b100;">unless</span> <span style="color: #339933;">-</span>f <span style="color: #0000ff;">$filename</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #339933;">-</span>f <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>INFILE<span style="color: #339933;">,</span> <span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Error reading file: $!&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$js</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;INFILE&gt;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>INFILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/open.html"><span style="color: #000066;">open</span></a><span style="color: #009900;">&#40;</span>OUTFILE<span style="color: #339933;">,</span> <span style="color: #ff0000;">'&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Error writing file: $!&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> OUTFILE JavaScript<span style="color: #339933;">::</span><span style="color: #006600;">Minifier</span><span style="color: #339933;">::</span><span style="color: #006600;">XS</span><span style="color: #339933;">::</span><span style="color: #006600;">minify</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$js</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/close.html"><span style="color: #000066;">close</span></a><span style="color: #009900;">&#40;</span>OUTFILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">send_http_header</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'application/javascript'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">sendfile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> OK<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<p>Por fim, edite a configuração do seu vhost no nginx e adicione o seguinte:</p>
<div class="codecolorer-container bash railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#Perl</span><br />
perl_modules <span style="color: #c20cb9; font-weight: bold;">perl</span>;<br />
perl_require Minify.pm;<br />
<br />
server <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; listen &nbsp; <span style="color: #000000;">80</span>;<br />
&nbsp; &nbsp; server_name www.nome.pt;<br />
&nbsp; &nbsp; root &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>nome.pt<span style="color: #000000; font-weight: bold;">/</span>public;<br />
<br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; a sua configuração existente<br />
&nbsp; &nbsp; ...<br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Static files location</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; location ~<span style="color: #000000; font-weight: bold;">*</span> ^.+.<span style="color: #7a0874; font-weight: bold;">&#40;</span>jpg<span style="color: #000000; font-weight: bold;">|</span>jpeg<span style="color: #000000; font-weight: bold;">|</span>gif<span style="color: #000000; font-weight: bold;">|</span>png<span style="color: #000000; font-weight: bold;">|</span>ico<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Perl Minify</span><br />
&nbsp; &nbsp; location ~ \.css$ <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; try_files <span style="color: #007800;">$uri</span>.min.css <span style="color: #000000; font-weight: bold;">@</span>minify_css;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
&nbsp; &nbsp; location ~ \.js$ <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; try_files <span style="color: #007800;">$uri</span>.min.js <span style="color: #000000; font-weight: bold;">@</span>minify_js;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
&nbsp; &nbsp; location <span style="color: #000000; font-weight: bold;">@</span>minify_css <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">perl</span> Minify::css_handler;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
&nbsp; &nbsp; location <span style="color: #000000; font-weight: bold;">@</span>minify_js <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">perl</span> Minify::js_handler;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.nunoneves.com/2012/01/minificar-css-e-javascript-com-o-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

