Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Can someone explain why they say this in the nginx documentation? Why is it "cumbersome and ineffective"?

A redirect to a main site

People who during their shared hosting life used to configure everything using only Apache’s .htaccess files, usually translate the following rules:

RewriteCond  %{HTTP_HOST}  nginx.org
RewriteRule  (.*)          http://www.nginx.org$1
to something like this:

server {
    listen       80;
    server_name  www.nginx.org  nginx.org;
    if ($http_host = nginx.org) {
        rewrite  (.*)  http://www.nginx.org$1;
    }
    ...
}

This is a wrong, cumbersome, and ineffective way. The right way is to define a separate server for nginx.org:

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

server {
    listen       80;
    server_name  www.nginx.org;
    ...
}
share|improve this question
add comment

1 Answer

Because every time you hit the site in the first option it will recheck the host header if to redirect you or not. and on the second it will not happen, sending some cpu cycles to garbage.

:)

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.