How to remove index files from URL on Nginx

So yesterday while i was idling on Freenode i saw someone asking in Nginx channel that he basically wants to remove the index.php file from the url or in other words he wants to redirect http://www.mydomain.com/index.php into http://www.mydomain.com/ … probably for canonicalization purpose (canonical url) and/or perhaps SEO purpose. He had tried this:

# And it causes redirect loop (also tested on my local box)
# Atlhough it works fine for simple php files but not on his vBulletin setup
# or other non simple php file (because i don't have access to vBulletin i tested it on other script)
# Note the | is for illustration
location =|~ /index.php {
	rewrite ^ http://www.domain.com/|/ permanent;
}

And being curious, i tried some testing to remove that index.php thing from the URL and found a way to do it and it was simple. So if you’re currently looking for a way to remove or redirect your index file from the URL and you’re using Nginx perhaps this simple tips can help you with that

First thing first is open your nginx configuration files that hold your virtual hosts configuration and then add this line inside the server directive: