php - How to configure rewrite rules for small api using nginx? -


i'm new nginx , have question in regards rewrite rules. i'm trying configure rewrite rules small api, there in way more suitable way of doing have come with?

i have php running behind it, using kind mvc workflow controller, method , argument taken uri, first param represent api key.

php concept:

controller->method( arguments ) 

configuration:

location /api/ {     # passing api key     rewrite ^/api/([0-9\-]+)/?$ /api/index.php?apikey=$1 last;     # passing api key , controller     rewrite ^/api/([0-9\-]+)/([0-9a-za-z\-]+)/?$ /api/index.php?apikey=$1&controller=$2 last;     # passing api key, controller , method     rewrite ^/api/([0-9\-]+)/([0-9a-za-z\-]+)/((select|insert|delete)+)/?$ /api/index.php?apikey=$1&controller=$2&method=$3 last;     # passing api key, controller, method , arguments     rewrite ^/api/([0-9\-]+)/([0-9a-za-z\-]+)/((select|insert|delete)+)/([0-9\-]+)/?$ /api/index.php?apikey=$1&controller=$2&method=$3&argument=$4 last; } 

thanks in advance,

if define rewrite rule every api request, end messy, hard-to-read nginx configuration file. moreover, still have define same routes in php.

therefore, in opinion makes sense have 1 general purpose rewrite, eg.:

location /api/ {     # passing api requests     rewrite ^/api/(.*)/?$ /api/index.php?uri=$1 last; } 

and handle logic in index.php – parse $_request['uri'] , route accordingly. if redirecting same file, why implement same logic twice?


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -