<?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/"
				  >
<channel>
<title><![CDATA[WEB-d - Développement Web]]></title>
<link><![CDATA[http://www.web-d.be/rss.xml]]></link>
<description><![CDATA[WEB-d - Développement Web]]></description>
<item>
<title><![CDATA[Accélérateur web Varnish]]></title>
<link><![CDATA[http://www.web-d.be/post/110/accélérateur-web-varnish.html]]></link>
<pubDate>Fri, 11 May 2012 14:47:04 +0200</pubDate>
<description><![CDATA[<p>Varnish Cache est un accélérateur de site web (web application accelerator), aussi appelé "caching reverse proxy". Il s'installe devant un ou plusieurs serveurs web afin d'intercepter les requêtes HTTP et, si possible, fournir une réponse directement depuis son cache, sans devoir interroger les serveurs web.</p>
<p style="text-align: center"><iframe width="560" height="315" src="http://www.youtube.com/embed/x7t2Sp174eI?rel=0" frameborder="0" allowfullscreen=""></iframe></p>
<h3>Installation</h3>
<p>Pour l'installer sur Ubuntu, il faut d'abord ajouter la clé du repository:</p>
<p><code><br />
curl&nbsp;http://repo.varnish-cache.org/debian/GPG-key.txt&nbsp;&gt;&nbsp;key.txt<br />
sudo&nbsp;apt-key&nbsp;add&nbsp;key.txt<br /></code></p>
<p>Il faut ensuite modifier le fichier /etc/apt/sources.list, et ajouter la ligne suivante:</p>
<p><code>deb&nbsp;http://repo.varnish-cache.org/ubuntu/&nbsp;lucid&nbsp;varnish-3.0</code></p>
<p>Et finalement l'installer de manière classique:</p>
<p><code><br />
sudo&nbsp;apt-get&nbsp;update<br />
sudo&nbsp;apt-get&nbsp;install&nbsp;varnish<br /></code></p>
<h3>Configuration</h3>
<p>Pour la configuration ci-dessous, Varnish est installé sur la même machine que le serveur web Apache. Varnish écoute sur le port 80, et Apache sur le port 8080.</p>
<p>Pour modifier les options de démarrage de Varnish, il faut modifier le fichier /etc/default/varnish:</p>
<p><code><br />
DAEMON_OPTS="-a&nbsp;:80&nbsp;\<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-T&nbsp;localhost:6082&nbsp;\<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-f&nbsp;/etc/varnish/default.vcl&nbsp;\<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-S&nbsp;/etc/varnish/secret&nbsp;\<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-s&nbsp;malloc,256m"<br /></code></p>
<ul>
<li>-a est le port sur lequel Varnish doit écouter</li>
<li>-s indique le mécanisme à utiliser pour le cache, ainsi que sa taille (256MB dans l'exemple)</li>
</ul>
<p>Il faut ensuite, dans le fichier /etc/varnish/default.vcl, indiquer à Varnish où se trouvent le ou les serveurs web. Dans ce cas-ci, il se trouve sur la même machine (127.0.0.1) et sur le port 8080:</p>
<p><code><br />
backend&nbsp;default&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.host&nbsp;=&nbsp;"127.0.0.1";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.port&nbsp;=&nbsp;"8080";<br />
}<br /></code></p>
<p>Par défaut, Varnish utilise une politique de mise en cache très prudente. Il ne met pas le contenu en cache si le browser envoie un cookie dans sa requête. Comme la plupart des sites utilisent les sessions et que le numéro de session est généralement stocké dans un cookie, le browser enverra le numéro de session pour chaque requête, donc également pour télécharger les images, fichiers vidéos, javascript, css,... ce qui empêche la mise en cache.</p>
<p>Comme règle de base, il faut autoriser Varnish à mettre en cache toutes les photos, les fichiers CSS et Javascript etc. malgré la présence d'un éventuel cookie dans la requête. Toujours dans le fichier /etc/varnish/default.vcl, il faut ajouter:</p>
<p><code><br />
#&nbsp;Si&nbsp;l'url&nbsp;se&nbsp;termine&nbsp;par&nbsp;jpg/png/...&nbsp;regarder&nbsp;d'abord&nbsp;dans&nbsp;le&nbsp;cache<br />
sub&nbsp;vcl_recv&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(req.http.Cache-Control&nbsp;~&nbsp;"private")&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(pass);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(req.url&nbsp;~&nbsp;"\.(png|gif|jpg|swf|css|js)$")&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(lookup);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
#&nbsp;Lorsqu'on&nbsp;a&nbsp;reçu&nbsp;la&nbsp;réponse&nbsp;du&nbsp;serveur,&nbsp;supprimer&nbsp;les&nbsp;cookies<br />
#&nbsp;éventuels&nbsp;avant&nbsp;d'enregistrer&nbsp;dans&nbsp;le&nbsp;cache<br />
sub&nbsp;vcl_fetch&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(beresp.http.Cache-Control&nbsp;~&nbsp;"private")&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(hit_for_pass);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(req.url&nbsp;~&nbsp;"\.(png|gif|jpg|swf|css|js)$")&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset&nbsp;beresp.http.set-cookie;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br /></code></p>
<h3>Configurer Apache</h3>
<p>Pour que Apache écoute sur le port 8080 au lieu du classique port 80, il faut d'abord modifier le fichier /etc/apache2/ports.conf:</p>
<p><code><br />
NameVirtualHost&nbsp;*:8080<br />
Listen&nbsp;8080<br /></code></p>
<p>Il faut ensuite modifier tous les virtual-hosts (tous les fichiers /etc/apache2/sites-enabled/...):</p>
<p><code><br />
&lt;VirtualHost&nbsp;*:8080&gt;<br />
...<br /></code></p>
<h3>Démarrage et test</h3>
<p>On peut enfin redémarrer Apache, et démarrer Varnish:</p>
<p><code><br />
sudo&nbsp;/etc/init.d/apache&nbsp;restart<br />
sudo&nbsp;/etc/init.d/varnish&nbsp;start<br /></code></p>
<p>Pour vérifier que Apache fonctionne correctement, il suffit d'ajouter :8080 dans l'url, par exemple http://mon.serveur.com:8080/ma/page.html</p>
<p>Varnish possède également plusieurs utilitaires pour tester son fonctionnement. Par exemple, pour savoir quelles URLs sont demandées par les clients au serveur Varnish:</p>
<p><code>varnishtop&nbsp;-i&nbsp;rxurl</code></p>
<p>Pour savoir quelles requêtes sont transmises par Varnish au serveur web (Apache):</p>
<p><code>varnishtop&nbsp;-i&nbsp;txurl</code></p>
]]></description>
</item>
<item>
<title><![CDATA[Autoriser la mise en cache de contenu dynamique PHP]]></title>
<link><![CDATA[http://www.web-d.be/post/151/autoriser-la-mise-en-cache-de-contenu-dynamique-php.html]]></link>
<pubDate>Thu, 10 May 2012 17:27:02 +0200</pubDate>
<description><![CDATA[<p>Lorsque vous utilisez PHP pour créer du contenu dynamique, que ce soit du code HTML, CSS ou Javascript, ou une image, PHP envoie par défaut au navigateur les headers</p>
<ul>
<li>Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0</li>
<li>Pragma: no-cache</li>
<li>Une date d'expiration (Expires) dans le passé.</li>
</ul>
<p style="text-align: center"><img src="/data/uploads/52_php-default-response-headers.png" alt="Headers PHP par défaut" /></p>
<p>Ces headres demandent au navigateur de l'utilisateur, ainsi qu'aux éventuels proxys, de ne pas mettre ce contenu en cache, et de chaque fois le télécharger depuis le serveur. Ceci va naturellement ralentir le site web, et peut également surcharger le serveur.</p>
<p>Pour demander la mise en cache du contenu, il faut utiliser les headers suivants:</p>
<p><code class="php"><br />
<span style="color: #000088;">$delais</span> <span style="color: #339933;">=</span> 60 <span style="color: #339933;">*</span> 60 <span style="color: #339933;">*</span> 24 <span style="color: #339933;">*</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// Une semaine</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"Pragma: public"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"Cache-Control: maxage="</span><span style="color: #339933;">.</span><span style="color: #000088;">$delais</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"Expires: "</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'D, d M Y H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$delais</span><span style="color: #009900;">)</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">" GMT"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><br />
&nbsp;</code></p>
]]></description>
</item>
<item>
<title><![CDATA[Centrer verticalement une image]]></title>
<link><![CDATA[http://www.web-d.be/post/34/centrer-verticalement-une-image.html]]></link>
<pubDate>Thu, 10 May 2012 17:10:40 +0200</pubDate>
<description><![CDATA[<p>Il existe plusieurs techniques, plus ou moins efficaces, pour centrer verticalement une image dans un div (ou autre). L'une des plus efficaces consiste à utiliser l'attribut CSS background:</p>
<p><code class="html"><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"</span><br />
<span style="color: #009900;">&nbsp;background: url(image.jpg) center center no-repeat;"</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp;</code></p>
<p>Il est même possible, grâce à CSS3, de redimensionner automatiquement l'image à la taille du div:</p>
<p style="text-align: center"></p>
<div style=" border: 1px solid #AAA; margin: 0 auto; max-width: 259px; height: 259px; background: #ddd url(http://t2.gstatic.com/images?q=tbn:ANd9GcTmhLMr4xUlv3RwhXzptCHND9-Av8aOnepJ7JhFMqWj8H5RkeaqBN7bEzWu) center center no-repeat; background-size: contain;"></div>
<p><code class="html"><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"</span><br />
<span style="color: #009900;">&nbsp;background: url(image.jpg) center center no-repeat;</span><br />
<span style="color: #009900;">&nbsp;background-size: contain;"</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp;</code></p>
]]></description>
</item>
<item>
<title><![CDATA[Un design réactif (responsive) grâce aux media queries]]></title>
<link><![CDATA[http://www.web-d.be/post/147/un-design-réactif-(responsive)-grâce-aux-media-queries.html]]></link>
<pubDate>Fri, 04 May 2012 21:55:17 +0200</pubDate>
<description><![CDATA[<p>Dans <a href="http://googlewebmastercentral.blogspot.com/2012/04/responsive-design-harnessing-power-of.html">un article posté récemment sur le blog Google Webmaster Central</a>, l'équipe de Google présente leurs techniques pour réaliser un design réactif (responsive):</p>
<ol>
<li>Pour les conteneurs, utiliser max-width et min-height au lieu de width et height, pour que la page puisse s'adapter lorsque la taille de la fenêtre diminue</li>
<li>Pour éviter que les images ne débordent de la fenêtre lorsque celle-ci est réduite, ajouter le code suivant à la feuille de style css: <code class="css"><br />
img <span style="color: #00AA00;">{</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">max-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">}</span><br />
&nbsp;</code></li>
<li>Les navigateurs de smartphones "simulent" généralement un écran plus grand. Pour les forcer à utiliser leur résolution réelle, il suffit d'ajouter dans la section head de la page html: <code class="html"><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"viewport"</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"width=device-width, initial-scale=1"</span>&gt;</span><br />
&nbsp;</code></li>
<li>Finalement, grâce aux media queries CSS3, il est possible d'ajouter du code css pour certaines tailles d'écran seulement: <code class="css"><br />
<span style="color: #808080; font-style: italic;">/* Code CSS de base */</span><br />
<br />
<span style="color: #a1a100;">@media screen and (min-width:480px) and (max-width:800px) {</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* Tablettes, et smartphones disposant d'un grand écran */</span><br />
<span style="color: #00AA00;">}</span><br />
<br />
<span style="color: #a1a100;">@media screen and (max-width:479px) {</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* Autres smartphones */</span><br />
<span style="color: #00AA00;">}</span><br />
&nbsp;</code></li>
</ol>
]]></description>
</item>
<item>
<title><![CDATA[Désactiver le mode compatibilité (compatibility) de Internet Explorer]]></title>
<link><![CDATA[http://www.web-d.be/post/146/désactiver-le-mode-compatibilité-(compatibility)-de-internet-explorer.html]]></link>
<pubDate>Thu, 03 May 2012 18:57:59 +0200</pubDate>
<description><![CDATA[<p>Internet Explorer possède un mode "compatibilité" qui peut être utilisé pour que les vieux sites s'affichent correctement.</p>
<p style="text-align: center"><img src="http://www.bfinternet.co.uk/news-info/wp-content/uploads/2009/09/ie8_compatibility.jpg" alt="Internet Explorer compatibility mode" /></p>
<p>Malheureusement, si un utilisateur active ce mode par erreur, un site récent risque de s'afficher de manière complètement erronée. Pour éviter ce désagrément, il est possible de désactiver le mode "compatibilité" en ajoutant la balise meta "X-UA-Compatible" avant toutes les autres balises meta:</p>
<p><code class="html"><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Ma page web<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"X-UA-Compatible"</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"IE=edge"</span> &gt;</span><br />
<br />
&nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- autres balises meta --&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp;</code></p>
<p>Il est également possible d'émuler une version spécifique de Internet Explorer:</p>
<p><code class="html"><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"X-UA-Compatible"</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"IE=EmulateIE7"</span> &gt;</span><br />
&nbsp;</code></p>
]]></description>
</item>
</channel>
</rss>
