The Computer Oracle

Nginx rewrite rule to remove path node

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation

--

Chapters
00:00 Nginx Rewrite Rule To Remove Path Node
00:37 Accepted Answer Score 34
01:08 Answer 2 Score 2
01:42 Thank you

--

Full question
https://superuser.com/questions/435916/n...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#nginx #urlrewriting

#avk47



ACCEPTED ANSWER

Score 34


Try this one:

location /blog {
 rewrite ^/blog(/.*)$ $1 last;
}

If you need this for more than one site you can't just put it higher in hierarchy because "location" clause can't be specified globally, only for specific site. If you need to add this clause for two sites or more you can put it another config file and then just "include" it in each site that needs this redirection.




ANSWER 2

Score 2


Depending where you define the rewrite directive you have two ways to implement it:

A. In the server context

server {
    ...
    rewrite ^/blog(/.*)$ $1 last;
    return 403;
    ...
}

B. In the location context

location /blog {
    rewrite ^/blog(/.*)$ $1 break;
}

Teo, why did you change the flag to break?* Because, if this directive is put inside of a location context, the last flag might make nginx to run 10 cycles and return the 500 error.