Why is redirecting inside the nginx config file inefficient?

0

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;
    ...
}

Dave

Posted 2011-10-29T18:48:50.063

Reputation: 257

Answers

1

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.

:)

Marcelo Bittencourt

Posted 2011-10-29T18:48:50.063

Reputation: 374