Move your app from one server to other without downtime (for reading)
So, you want to move from one server to another? But it can take time, from few minutes to hours. Or if you are going to move to other hoster, you will need backup database and import which can take few hours.
So what to do if you don't wanna users to stair at black screen and search agents to fail to get pages?
If you use laravel you can block any no-get request with error that site is in read-only mode for few minutes/hours.
- Turn off your website / server
- Make mysqldump / snapshot of your website
- Turn on your website / server with middleware
- In
app\Http\Kernel.php
addBlockPostRequest
middleware to all groups (web, api, etc).
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class BlockPostRequest
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->method() !== 'GET') {
abort(400, "Website is in read-only mode for few minutes");
}
return $next($request);
}
}
If you use other framework I guess there is something similar. Alternatively you can create new database user without write/update/delete rights and temporarily change configuration to use this readonly user.
Now you can move without panic as at least users can read your website, and in most cases (97%) visitors doesn't write anything.