CINXE.COM
Redirect www to non-www on nginx - Mattias Hising - Product and Web Developer
<!DOCTYPE html> <!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]--> <!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]--> <!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]--> <head><script type="text/javascript" src="/_static/js/bundle-playback.js?v=HxkREWBo" charset="utf-8"></script> <script type="text/javascript" src="/_static/js/wombat.js?v=txqj7nKC" charset="utf-8"></script> <script>window.RufflePlayer=window.RufflePlayer||{};window.RufflePlayer.config={"autoplay":"on","unmuteOverlay":"hidden"};</script> <script type="text/javascript" src="/_static/js/ruffle/ruffle.js"></script> <script type="text/javascript"> __wm.init("https://web.archive.org/web"); __wm.wombat("http://hising.net:80/2011/03/redirect-www-to-non-www-on-nginx.html","20121217091848","https://web.archive.org/","web","/_static/", "1355735928"); </script> <link rel="stylesheet" type="text/css" href="/_static/css/banner-styles.css?v=S1zqJCYt" /> <link rel="stylesheet" type="text/css" href="/_static/css/iconochive.css?v=3PDvdIFv" /> <!-- End Wayback Rewrite JS Include --> <meta charset="utf-8"> <title>Redirect www to non-www on nginx - Mattias Hising - Product and Web Developer</title> <meta name="author" content="Mattias"> <meta name="description" content="Logging this as a note for myself (and eventually someone else) so I dont forget it. Solves the problem of coexisting contexts www and non-www. With …"> <!-- http://t.co/dKP3o1e --> <meta name="HandheldFriendly" content="True"> <meta name="MobileOptimized" content="320"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="canonical" href="https://web.archive.org/web/20121217091848/http://hising.net/2011/03/redirect-www-to-non-www-on-nginx.html"> <link href="/web/20121217091848im_/http://hising.net/favicon.png" rel="icon"> <link href="/web/20121217091848cs_/http://hising.net/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css"> <script src="/web/20121217091848js_/http://hising.net/javascripts/modernizr-2.0.js"></script> <script src="/web/20121217091848js_/http://hising.net/javascripts/ender.js"></script> <script src="/web/20121217091848js_/http://hising.net/javascripts/octopress.js" type="text/javascript"></script> <link href="/web/20121217091848/http://hising.net/atom.xml" rel="alternate" title="Mattias Hising - Product and Web Developer" type="application/atom+xml"> <!--Fonts from Google"s Web font directory at http://google.com/webfonts --> <link href="https://web.archive.org/web/20121217091848cs_/http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"> <link href="https://web.archive.org/web/20121217091848cs_/http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css"> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-186744-31']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://web.archive.org/web/20121217091848/https://ssl' : 'https://web.archive.org/web/20121217091848/http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </head> <body> <header role="banner"><hgroup> <h1><a href="/web/20121217091848/http://hising.net/">Mattias Hising - Product and Web Developer</a></h1> <h2>How to build profitable high performance web products that user loves</h2> </hgroup> </header> <nav role="navigation"><ul class="subscription" data-subscription="rss"> <li><a href="/web/20121217091848/http://hising.net/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li> </ul> <form action="https://web.archive.org/web/20121217091848/http://google.com/search" method="get"> <fieldset role="search"> <input type="hidden" name="q" value="site:hising.net"/> <input class="search" type="text" name="q" results="0" placeholder="Search"/> </fieldset> </form> <ul class="main-navigation"> <li><a href="/web/20121217091848/http://hising.net/">Blog</a></li> <li><a href="/web/20121217091848/http://hising.net/blog/archives">Archives</a></li> </ul> </nav> <div id="main"> <div id="content"> <div> <article class="hentry" role="article"> <header> <h1 class="entry-title">Redirect Www to Non-www on Nginx</h1> <p class="meta"> <time datetime="2011-03-02T07:21:19+01:00" pubdate data-updated="true">Mar 2<span>nd</span>, 2011</time> </p> </header> <div class="entry-content"><p>Logging this as a note for myself (and eventually someone else) so I dont forget it. Solves the problem of coexisting contexts www and non-www. With this all requests to www.* are redirected to * on the nginx webserver</p> <pre><code><code>server { listen 80; server_name www.domain.com domain.com; root /var/www/domain.com/public_html; index /index.html; if ($host = 'www.domain.com' ) { rewrite ^/(.*)$ http://domain.com/$1 permanent; } location / { #whatever } location ~ /\.ht { deny all; } }</code> </code></pre> <p>Update. <a href="https://web.archive.org/web/20121217091848/http://wiki.nginx.org/Pitfalls">According to Nginx</a> the above code is a common pitfall, and should instead read:</p> <pre><code><code>server { server_name www.domain.com; rewrite ^ $scheme://domain.com$request_uri redirect; } server { listen 80; server_name domain.com; root /var/www/domain.com/public_html; index /index.html; location / { #whatever } }</code> </code></pre> <blockquote><p>There are actually three problems here. The first being the if. That’s what we care about now. Why is this bad? Did you read <a href="https://web.archive.org/web/20121217091848/http://wiki.nginx.org/IfIsEvil">If is Evil</a>? When nginx receives a request no matter what is the subdomain being requested, be it www.domain.com or just the plain domain.com this if directive is always evaluated. Since you’re requesting nginx to check for the Host header for every request. It’s extremely inefficient. You should avoid it. Instead use two server directives like the example below.</p></blockquote> <p>Thanks to Johan as well who pointed me in this direction in his comments. However, if you use the first setup, I dont think you have to hurry to change it, it is almost certain of academic value on 90%+ of all nginx-setups.</p> </div> <footer> <p class="meta"> <span class="byline author vcard">Posted by <span class="fn">Mattias</span></span> <time datetime="2011-03-02T07:21:19+01:00" pubdate data-updated="true">Mar 2<span>nd</span>, 2011</time> <span class="categories"> <a class="category" href="/web/20121217091848/http://hising.net/category/web-development/">Web Development</a> </span> </p> <div class="sharing"> <a href="https://web.archive.org/web/20121217091848/http://twitter.com/share" class="twitter-share-button" data-url="https://web.archive.org/web/20121217091848/http://hising.net/2011/03/redirect-www-to-non-www-on-nginx.html" data-via="hising" data-counturl="http://hising.net/2011/03/redirect-www-to-non-www-on-nginx.html">Tweet</a> <div class="g-plusone" data-size="medium"></div> <div class="fb-like" data-send="true" data-width="450" data-show-faces="false"></div> </div> <p class="meta"> <a class="basic-alignment left" href="/web/20121217091848/http://hising.net/2011/02/my-upcoming-posts.html" title="Previous Post: My Upcoming Posts">« My Upcoming Posts</a> <a class="basic-alignment right" href="/web/20121217091848/http://hising.net/2011/03/web-performance-django-cms-and-smileys.html" title="Next Post: Web Performance, Django CMS and Smileys">Web Performance, Django CMS and Smileys »</a> </p> </footer> </article> </div> <aside class="sidebar"> <section> <h1>About</h1> <p>I run my own business, <a href="https://web.archive.org/web/20121217091848/http://ridgestreet.com/">80 Ridge Street Media</a> (<a href="https://web.archive.org/web/20121217091848/http://ridgestreet.se/">Svenska</a>). I build profitable web products. I am available for consulting in User Experience, JavaScript, Python, Product Management and Web Performance Tuning. Send me an email at <a href="https://web.archive.org/web/20121217091848/mailto:mattias@rdg.st">mattias@rdg.st</a> if you are interested in discussing consultant gigs.</p> </section> <section> <h1>Recent Posts</h1> <ul id="recent_posts"> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/11/fixing-sass-watch-on-os-x-lion.html">Fixing SASS --watch on OS X Lion</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/11/mumrik-2-work-has-started.html">Mumrik 2 - Work has started</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/06/if-developers-were-to-try-a-real-job-carpenter.html">If Developers Were To Try a Real Job - Carpenter</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/04/action-is-the-killer-app.html">Action is the Killer App</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/03/you-cant-fight-youth.html">You Can't Fight Youth</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/03/web-performance-django-cms-and-smileys.html">Web Performance, Django CMS and Smileys</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/03/redirect-www-to-non-www-on-nginx.html">Redirect www to non-www on nginx</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/02/my-upcoming-posts.html">My Upcoming Posts</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/02/how-to-get-started-on-html5.html">How to get started on HTML5</a> </li> <li class="post"> <a href="/web/20121217091848/http://hising.net/2011/02/my-thoughts-on-apple-subscriptions.html">My Thoughts on Apple Subscriptions</a> </li> </ul> </section> <section> <h1>GitHub Repos</h1> <ul id="gh_repos"> <li class="loading">Status updating...</li> </ul> <a href="https://web.archive.org/web/20121217091848/https://github.com/hising">@hising</a> on GitHub <script type="text/javascript"> $.domReady(function(){ if (!window.jXHR){ var jxhr = document.createElement('script'); jxhr.type = 'text/javascript'; jxhr.src = '/javascripts/libs/jXHR.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jxhr, s); } github.showRepos({ user: 'hising', count: 0, skip_forks: true, target: '#gh_repos' }); }); </script> <script src="/web/20121217091848js_/http://hising.net/javascripts/github.js" type="text/javascript"> </script> </section> <section> <h1>Latest Tweets</h1> <ul id="tweets"> <li class="loading">Status updating...</li> </ul> <script type="text/javascript"> $.domReady(function(){ getTwitterFeed("hising", 4, false); }); </script> <script src="/web/20121217091848js_/http://hising.net/javascripts/twitter.js" type="text/javascript"> </script> <a href="https://web.archive.org/web/20121217091848/http://twitter.com/hising" class="twitter-follow-button" data-show-count="true">Follow @hising</a> </section> <section class="googleplus"> <h1> <a href="https://web.archive.org/web/20121217091848/https://plus.google.com/108159897663209173007?rel=author"> <img src="https://web.archive.org/web/20121217091848im_/http://www.google.com/images/icons/ui/gprofile_button-32.png" width="32" height="32"> Google+ </a> </h1> </section> </aside> </div> </div> <footer role="contentinfo"><p> Copyright © 2012 - Mattias - <span class="credit">Powered by <a href="https://web.archive.org/web/20121217091848/http://octopress.org/">Octopress</a></span> </p> </footer> <script type="text/javascript"> var disqus_shortname = 'hising'; var disqus_script = 'count.js'; (function () { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); }()); </script> <div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = "//web.archive.org/web/20121217091848/http://connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <script type="text/javascript"> (function() { var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true; script.src = 'https://web.archive.org/web/20121217091848/https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s); })(); </script> <script type="text/javascript"> (function(){ var twitterWidgets = document.createElement('script'); twitterWidgets.type = 'text/javascript'; twitterWidgets.async = true; twitterWidgets.src = 'https://web.archive.org/web/20121217091848/http://platform.twitter.com/widgets.js'; document.getElementsByTagName('head')[0].appendChild(twitterWidgets); })(); </script> </body> </html> <!-- FILE ARCHIVED ON 09:18:48 Dec 17, 2012 AND RETRIEVED FROM THE INTERNET ARCHIVE ON 17:59:28 Nov 28, 2024. JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE. ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C. SECTION 108(a)(3)). --> <!-- playback timings (ms): captures_list: 1.165 exclusion.robots: 0.063 exclusion.robots.policy: 0.037 esindex: 0.019 cdx.remote: 33.174 LoadShardBlock: 442.54 (3) PetaboxLoader3.datanode: 165.719 (4) PetaboxLoader3.resolve: 235.27 (2) load_resource: 211.261 -->