Nginx rewrite equivalent to Apache RewriteRule tha

2019-07-08 02:26发布

问题:

I'm performing a migration from an Apache server to Nginx and missed a .htaccess file that performs a rewrite:

RewriteRule ^(.*)$ routes.php?params=$1 [NC,QSA]

For example, this endpoint example.com/api/v1/param1/param2/param3 would be rewritten as https://example.com/api/v1/routes.php?params=/param1/param2/param3

Can someone confirm that this is the correct equivalent for Nginx before I re-attempt the migration?

rewrite "(?i)^(.*)$" routes.php?params=$1

and is this how it would be used in the config file since /api/v1 is the only path that requires the rewrite?

location /api/v1 {
  rewrite "(?i)^(.*)$" routes.php?params=$1
} 

UPDATE

Adding this to the conf file in Laravel Forge appears to just break the application and prevents displaying any views. Instead, it says This site can’t be reached example.com refused to connect.

回答1:

All nginx URIs begin with a leading /, and your regular expressions do not attempt to remove the /api/v1/ prefix before appending the parameter list.

Try:

location /api/v1 {
    rewrite ^/api/v1(/.*)$ /api/v1/routes.php?params=$1 last;
}