Mastering Nginx

(Ron) #1

NGINX as a Reverse Proxy


[ 84 ]

}

if ($host ~* (www\.example\.org|www\.example\.net)) {

rewrite ^/(.*)$ http://www.example.com/$1 redirect;

}

}

Here, we have a number of if directives matching the Host header (or, if not


present, server_name). After each if, the URI is rewritten to lead directly to


the correct application component. Besides being terribly inefficient due to the
processing required to match each regular expression for every URI, it breaks


our "no ifs within a location" rule.


This type of configuration is better rewritten as a series of separate server contexts,


in which the URL is rewritten to the application component:


server {

server_name marketing.example.com marketing.example.org marketing.
example.net;

rewrite ^ http://www.example.com/marketing/application.do permanent;

}

server {

server_name communication.example.com communication.example.org
communication.example.net;

rewrite ^ http://www.example.com/comms/index.cgi permanent;

}

server {

server_name http://www.example.org http://www.example.net;

rewrite ^ http://www.example.com$request_uri permanent;
}
Free download pdf