简体   繁体   中英

Converting Rewrites from Apache to NGINX

I have a custom PHP application which follows the basic MVC pattern of development. The applications directory structure is as follows:

admin/   
default/
index.php

In the admin is another index.php (which is how it handles requests to /admin).

In apache, this worked by putting a .htaccess file in each directory (admin and doc root) setting up a rewite and the application works. In NGINX, it doesnt seem so simple.

I can get the basic "default" application to work, by using this in my nginx.conf:

if (!-e $request_filename)
{
    rewrite ^(.+)$ /index.php?path=$1 last;
}

(we run on vhosts so I cannot reference location / or I get a duplicate rule error).

This gets the frontend application to work, but when I try to access the /admin portion of the application the login screen loads, but when I try to submit and it tries to hit the endpoint 'admin/index/index' it fails, as my rewrite rule doesnt work. Here is what I have for the rewrite in NGINX:

location /admin {
    try_files $uri $uri/ /admin/index.php?path=$uri&$args;
}

I think the issue is that the $uri being passed in is /admin/index/index instead of it being what it should be and is under Apache: /index/index.

Can anyone help me correct these NGINX rules so that my application works properly?

Thanks in advance.

As you said, you pass in the extra $uri. Try this:

location /admin {
  try_files $uri $uri/ /admin/index.php?$args;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM