Nginx to redirect to specific PHP

I wanted to rewrite the url path to point to certain PHP. Here is the flow:

User Login

https://admin.abc.com

After they logged in, the system will pass url to be redirected

https://admin.abc.com/MX/Home

What it does here is the it is actually redirecting to main.php?url=Home, so I'm expecting the URL rewrite to rewrite to the above.

The physical path for MX/Home doesn't exist, but path towards MX exists. This main.php is inside the MX folder, here is the path:

/var/www/html/MX/main.php

So after login, it should redirect to main.php and rewrite to the url into like this

https://admin.abc.com/MX/Home

but it is actually main.php running the based on the url parameter. Below is my configuration but seems to unable achieve the above. Kindly advise.

server {
    listen 80;

    root /var/www/html;
    index login.php;
    server_name  admin.abc.com;

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

    location ^/MX {
      rewrite ^/MX/^(.*)$ /MX/main.php?url=$1 last;
    }

    location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }
}

The location ^/MX { ... } block is placed above the location ~ \.php$ { ... } block which means that the URI /MX/main.php will not be processed as a PHP file. In fact, you will probably get a redirection loop. See this document for more.

Either swap the order of the location blocks, or use a prefix location instead of a regular expression location.

For example:

location /MX {
    rewrite ^/MX/?(.*)$ /MX/main.php?url=$1 last;
}

The above location will process URIs beginning with /MX, but not those ending with .php. Also, there was a typo in your rewrite regular expression.