Chapter 8
[ 205 ]
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
The try_files directive is used to determine if the file exists on the filesystem,
and if not, passes the request on to the FastCGI server, without using if.
Using if as a hostname switch
There are countless examples of configurations where if is used to redirect requests
based on the HTTP Host header. These types of configurations work as selectors and
are evaluated for each request:
server {
server_name .example.com;
root /var/www/html;
if ($host ~* ^example\.com) {
rewrite ^/(.*)$ http://www.example.com/$1 redirect;
}
}
Instead of incurring the processing costs associated with evaluating if for each
request, NGINX's normal request-matching routine can route the request to the
correct virtual server. The redirect can then be placed where it belongs, and even
without a rewrite:
server {
server_name example.com;
return 301 $scheme://www.example.com;
}