haproxy-charset.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="generator" content="Hugo 0.36" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link rel="stylesheet" href="/assets/css/theme.css">
  8. <link rel="alternate" href="/rss.xml" type="application/rss+xml" title="Pleasant Programmer">
  9. <script type="text/javascript" src="//use.typekit.net/iwm5axp.js"></script>
  10. <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
  11. <title>Haproxy Charset - Pleasant Programmer</title>
  12. </head>
  13. <body>
  14. <header id="header" role="banner">
  15. <div id="thomas">
  16. <img src="/assets/img/thomas.gif" alt="DJ THOMAS IN DA HAUS">
  17. <img src="/assets/img/thomas.png" alt="Pleasant Programmer">
  18. </div>
  19. <h1 class="site-title"><a href="/">Pleasant Programmer</a></h1>
  20. <nav id="menu" role="navigation">
  21. <ul>
  22. <li class="twitter">
  23. <a href="http://twitter.com/pleasantprog">@pleasantprog</a>
  24. </li>
  25. <li><a href="/pages/projects.html">projects</a></li>
  26. <li><a href="/posts.html">archives</a></li>
  27. <li><a href="/tags.html">tags</a></li>
  28. <li><a href="/rss.xml">rss</a></li>
  29. </ul>
  30. </nav>
  31. </header>
  32. <div id="container">
  33. <main id="content" role="main">
  34. <article itemscope itemtype="http://schema.org/BlogPosting">
  35. <h1 class="p-name entry-title" itemprop="headline name">
  36. <a href="/posts/haproxy-charset.html">Haproxy Charset</a></h1>
  37. <small>
  38. <span class="dateline">Posted: <time itemprop="datePublished" datetime="2016-06-24">2016-06-24</time></span>
  39. | More posts about
  40. <a class="tag p-category" href="/tags/sysadmin.html" rel="tag">
  41. sysadmin
  42. </a>
  43. <a class="tag p-category" href="/tags/haproxy.html" rel="tag">
  44. haproxy
  45. </a>
  46. </small>
  47. <div class="e-content entry-content" itemprop="entry-text">
  48. <p>A common problem we encounter is for things like <em>ñ</em> not showing up correctly. This actually caused <a href="http://www.rappler.com/nation/politics/elections/2016/132894-human-error-hash-election-results-code-mismatch">some issues</a> in the recent Philippine elections, but this isn&rsquo;t about hash codes or anything like that.</p>
  49. <p>By default, we use UTF-8 for text storage and rendering. A problem is that browsers don&rsquo;t assume UTF-8 as the default and you need to have either a <code>&lt;meta charset=&quot;utf-8&quot; /&gt;</code> in the HTML or <code>Content-Type: text/html; charset=utf-8</code> in the headers. A few of our services don&rsquo;t set the <code>Content-Type</code> with the <code>charset=utf-8</code> part so you&rsquo;d get piñata instead of piñata.</p>
  50. <p>Being lazy, we usually just correct this at the reverse proxy side. It&rsquo;s trivial to do in nginx. You just need to add <code>charset utf-8;</code> to your configuration and you&rsquo;re good. For haproxy though, I couldn&rsquo;t readily find a solution for it and had to go through the docs to see what I could do.</p>
  51. <p>After a bit of experimenting, I had success with this:</p>
  52. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><code class="language-text" data-lang="text"><span></span># set content-type to utf-8 if not already
  53. acl has_charset hdr_sub(content-type) -i charset=
  54. rspirep (Content-Type.*) \1;\ charset=utf-8 unless has_charset
  55. </code></pre></div>
  56. <p>This is probably not the best way to do it. Arguably, we should just fix our services to have the correct <code>Content-Type</code> in the first place, but I can do that some other time.</p>
  57. </div>
  58. <aside class="postpromonav">
  59. <nav>
  60. <ul class="pager clearfix">
  61. <li class="previous">
  62. <a href="/posts/cloudflare-shenanigans.html" rel="prev" title="Cloudflare Shenanigans">&larr; Previous post</a>
  63. </li>
  64. <li class="next">
  65. <a href="/posts/openpreppad.html" rel="next" title="OpenPrepPad">Next post &rarr;</a>
  66. </li>
  67. </ul>
  68. </nav>
  69. </aside>
  70. <section class="comments">
  71. <script
  72. data-isso="https://isso.pleasantprogrammer.com/"
  73. data-isso-require-author="true"
  74. data-isso-vote="false"
  75. src="https://isso.pleasantprogrammer.com/js/embed.min.js">
  76. </script>
  77. <section id="isso-thread"></section>
  78. </section>
  79. </article>
  80. </main>
  81. <footer id="footer" role="contentinfo">
  82. <p>
  83. <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/deed.en_US">
  84. <img alt="CC-BY-SA" style="border-width:0" src="https://licensebuttons.net/l/by-sa/3.0/80x15.png">
  85. </a> &copy; 2018 Thomas Dy - Powered by <a href="http://gohugo.io">Hugo</a></p>
  86. </footer>
  87. </div>
  88. <script src="/assets/js/konami.js"></script>
  89. <script>
  90. var easter_egg = new Konami();
  91. easter_egg.code = function() {
  92. var el = document.getElementById('thomas');
  93. if(el.className == "whoa") {
  94. el.className = "";
  95. }
  96. else {
  97. el.className = "whoa";
  98. }
  99. document.body.scrollTop = document.documentElement.scrollTop = 0;
  100. }
  101. easter_egg.load();
  102. </script>
  103. </body>
  104. </html>