how to remove location block from $uri in nginx configuration?

Solution 1:

Looking around I would guess that using a regexp location with captures is the easiest. Adapting your example I end up with:

location ~ ^/cargo(.*) {
    try_files $1 $1/ /cargo/index.php?_REWRITE_COMMAND=$1&args;
}

Solution 2:

I found another thing that worked for me (as I'm using gunicorn, I can't choose what to pass)

You should be able to get away with

location /cargo {
    rewrite ^/cargo(.*)$ $1 break;
    try_files $uri $uri/ /cargo/index.php?_REWRITE_COMMAND=$uri&args;
}

Solution 3:

For those who might be struggling to add it for micro service or API with Node JS I used the following to remove api from the url on my server:

location ^~ /api {
        rewrite ^/api(/.*)$ $1 break;
        proxy_pass    http://127.0.0.1:3001/;
    }