Mastering Nginx

(Ron) #1

Rewrite Rule Guide


[ 276 ]

root /home/www/site1;

}

server {

server_name site2.example.com;

root /home/www/site2;

}

server {

server_name site3.example.com;

root /home/www/site3;

}

These are essentially different virtual hosts, so it is best to treat them as such in the
configuration as well.


Rule #4: Replace RewriteCond with if for variable checks


This rule applies only after having applied rules 1 to 3. If there are any remaining
conditions not covered by those rules, then if may be applied to test the values of


variables. Any HTTP variable may be used by prefixing the lowercased name of the
variable with $http_. If there are hyphens (-) in the name, these are translated into


underscores (_).


The following example (taken from Apache's documentation on the mod_rewrite


module at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html)) is


used to decide which page should be delivered to a client based on the User-Agent
header:


RewriteCond %{HTTP_USER_AGENT} ^Mozilla

RewriteRule ^/$ /homepage.max.html [L]

RewriteCond %{HTTP_USER_AGENT} ^Lynx
Free download pdf