Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to configure OpenResty (nginx) to redirect any non www domain to www. one

but the thing that i'm acheiving is this : www.www.domain.com

eh?

here are my server blocks

server {
    listen       80;
    server_name  ^$http_host$;
    return       301 http://www.$http_host$request_uri;
}

server {
    listen       80;
    server_name  ~^(www.)$$http_host;

    location = / {
        content_by_lua '
            ngx.say("Host: ", ngx.req.get_headers()["Host"])
        ';
    }
}

any help is greatly appreciated.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

after some trial and error , here is how i fixed it :

server {
    listen       80;
    server_name  $http_host;
    return       301 http://www.$http_host$request_uri;
}


server {
    listen       80;
    server_name  www.*;

    location = / {
        content_by_lua '
            ngx.say("Host: ", ngx.req.get_headers()["Host"])
        ';
    }
}
share|improve this answer
add comment

Your Answer

 
discard

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.