The simpler and preferred method for redirecting a request in Nginx is using the return directive. This will return a 301 code along with the new address.

However sometimes you may have a situation where the redirection is not permanent as implied by the 301.

During a maintenance outage you might have a page with information about the outage that you want to redirect users to. All requests should go to this page, but when the outage is over, if the user refreshes the page, they can still be returned to the appropriate location.

In the example below the rewrite directive will redirect any request for a resource other than root to /var/www/html/index.html. For example yoursite.com and yoursite.com/someresource both return the same page, but will keep the original request in the address bar.

server {
    listen 80;
    root /var/wwww/html;
    rewrite ^\/.+$ /index.html last;

    location / {
    }
}