Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been working with apache for years and now wanted to move to nGinx keeping in view the performance everyone on internet seems to be talking about. But I its very frustrating for me for not being able to rewrite a non-www url to www i.e. to

I have referred to all the posts on StackOverflow and have tried their configurations. Somehow the system does not seem to work. I have wasted more than 2 hours in this and need help from you. My nGinx version is 1.2.1. This was 15sec work in Apache. I have somehow started missing apache.

share|improve this question

migrated from Jul 16 '12 at 16:39

This question came from our site for professional and enthusiast programmers.

Taken from my site

server {
    listen [::]:80;
    return 301$request_uri;

Your other server block should not respond to, but only

share|improve this answer

do not worry...I could find a solution:

 if ($host = '' ) {
         rewrite  ^/(.*)$  permanent;
share|improve this answer
That is actually an inefficient solution as every single request to your site will be tested whether needed or not. Best solution is to create a separate server block with server_name and within that block, put return 301$request_uri; In this way, requests with the www will not have to waste time going through the if test. See: – Dayo Jul 14 '12 at 13:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.