Mod_rewrite for nginx rewrite rules

I converted most of my mod_rewrite Apache HTTPd rules to the nginx HttpRewrite module (which calls PHP-FPM via FastCGI on every dynamic request). Simple rules defined by hard locations work fine:

location = /favicon.ico { rewrite ^(.*)$ /_core/frontend.php?type=ico&file=include__favicon last; } 

I'm still having problems with regular expressions that are parsed in mod_rewrite like this (note that I accept trailing slashes as part of the rules, and also add a query string to each query):

mod_rewrite

 # File handler RewriteRule ^([a-z0-9-_,+=]+)\.([az]+)$ _core/frontend.php?type=$2&file=$1 [QSA,L] # Page handler RewriteRule ^([a-z0-9-_,+=]+)$ _core/frontend.php?route=$1 [QSA,L] RewriteRule ^([a-z0-9-_,+=]+)\/$ _core/frontend.php?route=$1 [QSA,L] RewriteRule ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)$ _core/frontend.php?route=$1/$2 [QSA,L] RewriteRule ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)\/$ _core/frontend.php?route=$1/$2 [QSA,L] RewriteRule ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)$ _core/frontend.php?route=$1/$2/$3 [QSA,L] RewriteRule ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)\/$ _core/frontend.php?route=$1/$2/$3 [QSA,L] 

I came up with the following server configuration for the site, but after analyzing the request (for example, GET /user/auth ), I met unrivaled rules:

nginx attempt to rewrite

 location / { # File handler rewrite ^([a-z0-9-_,+=]+)\.([az]+)?(.*)$ /_core/frontend.php?type=$2&file=$1&$3 break; # Page handler rewrite ^([a-z0-9-_,+=]+)(\/*)?(.*)$ /_core/frontend.php?route=$1&$2 break; rewrite ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)(\/*)?(.*)$ /_core/frontend.php?route=$1/$2&$3 break; rewrite ^([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)\/([a-z0-9-_,+=]+)(\/*)?(.*)$ /_core/frontend.php?route=$1/$2/$3&$4 break; } 

What would you recommend for working with my file handler (it's just filename.ext ) and my page handler (which is a unique route request with three properties defined by a forward slash)?

As I have not yet received a response from this, I am also not sure that this will override my PHP parser, which is defined using location ~ \.php {} , which is included before these rewrite rules.

Bonus points if I can solve parsing problems without having to use a new rule for each number of route properties.

+4
source share
1 answer

In the end, I wrote the following rules:

File handler

 location ~ ^/([a-zA-Z0-9-_]*)\.([a-zA-Z0-9]*)$ { include /web/_config/php.conf; rewrite ^/([a-zA-Z0-9-_]*)\.([a-zA-Z0-9]*)$ /_core/frontend.php?type=$2&file=$1 last; } 

The file handler captures the name and extension and writes it to type = {ext} & file = {name}.

Page handler

 location ~ ^/([a-z0-9-_]*)$ { include /web/_config/php.conf; rewrite ^/([a-z0-9-_]*)$ /_core/frontend.php?route=$1 last; } location ~ ^/([a-z0-9-_]*)/?([a-z0-9-_]*)$ { include /web/_config/php.conf; rewrite ^/([a-z0-9-_]*)/?([a-z0-9-_]*)$ /_core/frontend.php?route=$1/$2 last; } location ~ ^/([a-z0-9-_]*)/?([a-z0-9-_]*)/?([a-z0-9-_]*)$ { include /web/_config/php.conf; rewrite ^/([a-z0-9-_]*)/?([a-z0-9-_]*)/?([a-z0-9-_]*)$ /_core/frontend.php?route=$1/$2/$3 last; } 

The page handler (which in this case processes up to 3 "directories") captures the line between each delimiter (/) whether the regular expression executes and writes it as a query string.

The main difference between this and my initial configuration was that each record has its own location handler, and the last rule handles it in the first match, so performance should be a little better.

I also found that nginx adds query strings by default, so no regular expression is needed, another performance improvement.

Note that /web/_config/php.conf is just an end-to-end FastCGI configuration, and the one that comes with nginx (usually /etc/nginx/fastcgi.conf ) should work fine. Please note that if you are dealing exclusively with PHP, you do not need to define this in each rule, just add them to the include.

Hope this helps.

+2
source

All Articles