O'Reilly logo

Mastering NGINX by Dimitri Aivaliotis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Translating from Apache

There is a long history of writing rewrite rules for Apache's powerful mod_rewrite module, and most resources on the Internet are focused on these. When encountering rewrite rules in Apache's format, they can be translated into a form that NGINX can parse by following a few simple rules.

Rule #1: Replace directory and file existence checks with try_files

When encountering an Apache rewrite rule of the following form:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?q=$1 [L]

This can best be translated into an NGINX configuration as follows:

try_files $uri $uri/ /index.php?q=$uri;

These rules state that when the filename specified in the URI is neither a file nor a directory ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required