Mastering Nginx

(Ron) #1
Appendix B

[ 277 ]

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

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

This can be translated to an NGINX configuration as follows:


if ($http_user_agent ~* ^Mozilla) {

rewrite ^/$ /homepage.max.html break;

}

if ($http_user_agent ~* ^Lynx) {

rewrite ^/$ /homepage.min.html break;

}

index homepage.std.html;

If there are any special variables that are available only under Apache's mod_rewrite,


then these of course can't be checked in NGINX.


Summary


We explored NGINX's rewrite module in this appendix. There are only a few


directives associated with the module, but these can be used to create some complex


configurations. Taking the process of creating new rewrite rules step-by-step has
hopefully demonstrated how rewrite rules can be made easily. An understanding


of regular expressions, how to read and construct them, is needed before creating
rewrite rules of any complexity. We rounded this appendix off by examining how


to translate Apache-style rewrite rules into a configuration that NGINX can parse.


In doing so, we discovered that quite a few Apache rewrite rule scenarios can be
solved differently in NGINX.

Free download pdf