Laravel - artisan down / Maintenance Mode except own IP

后端 未结 6 1855
Happy的楠姐
Happy的楠姐 2021-02-14 16:38

Currently i am using Laravel5. My question is if if i use the Maintenance mode with

php artisan down

how can say \"the application is down for

相关标签:
6条回答
  • 2021-02-14 17:03

    Now you can use php artisan down --allow=127.0.0.1, or of course change the IP to something else. Multiple IPs are also supported, networks are also supported.

    Not sure when this was implemented but is working fine for me in 5.6

    0 讨论(0)
  • 2021-02-14 17:06

    Another solution to your problem would be to develop locally, so only you can see the development version of your application. The application which is visible by everyone, the one on your production server, could be set into maintenance mode until you're ready to show it to the world. You can then deploy your application to your server and turn off the maintenance mode.

    What I'm saying, is that automatising this process may not be needed, as you'll probably be present on your server when you're deploying your application, so you can manually turn the mode on and off.

    Developing local can be quite easy using WAMP/MAMP, Laravel's Homestead, Laravel Valet or for experts Laradock. Of course, you can use any.

    0 讨论(0)
  • 2021-02-14 17:12

    Create new middleware

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    
    use Illuminate\Contracts\Foundation\Application;
    
    use Illuminate\Http\Request;
    
    use Symfony\Component\HttpKernel\Exception\HttpException;
    
    
    
    class CheckForMaintenanceMode
    
    {
    
        protected $request;
    
        protected $app;
    
    
    
        public function __construct(Application $app, Request $request)
    
        {
    
            $this->app = $app;
    
            $this->request = $request;
    
        }
    
    
    
        /**
    
         * Handle an incoming request.
    
         *
    
         * @param  \Illuminate\Http\Request  $request
    
         * @param  \Closure  $next
    
         * @return mixed
    
         */
    
    
    
        public function handle($request, Closure $next)
    
        {
    
            if ($this->app->isDownForMaintenance() &&
    
                !in_array($this->request->getClientIp(), ['::1']))
    
            {
    
                throw new HttpException(503);
    
            }
    
    
    
            return $next($request);
    
        }
    
    }
    

    The '::1' is your own IP assuming your in localhost, if not the specify your IP. You can excluded multiple IP in the array. check Excluding your IP Address in Maintenance Mode (php artisan down) in Laravel 5

    0 讨论(0)
  • 2021-02-14 17:17
    $ php artisan down --allow=127.0.0.1
    
    0 讨论(0)
  • 2021-02-14 17:21

    For Laravel 8:

    Even while in maintenance mode, you may use the secret option to specify a maintenance mode bypass token:

    php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
    

    After placing the application in maintenance mode, you may navigate to the application URL matching this token and Laravel will issue a maintenance mode bypass cookie to your browser:

    https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515

    When accessing this hidden route, you will then be redirected to the / route of the application. Once the cookie has been issued to your browser, you will be able to browse the application normally as if it was not in maintenance mode.

    There some new nice features with maintenance mode in Laravel 8. Read it here.

    0 讨论(0)
  • 2021-02-14 17:26

    In Laravel 5 you have to create your own middleware. Create a file in app/Http/Middleware/CheckForMaintenanceMode.php You can choose of course any filename.

    <?php namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Contracts\Foundation\Application;
    use Illuminate\Http\Request;
    use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as MaintenanceMode;
    
    class CheckForMaintenanceMode {
    
        protected $app;
    
        public function __construct(Application $app)
        {
            $this->app = $app;
        }
    
        public function handle(Request $request, Closure $next)
        {
            if ($this->app->isDownForMaintenance() && 
                !in_array($request->getClientIp(), ['8.8.8.8', '8.8.4.4']))
            {
                $maintenanceMode = new MaintenanceMode($this->app);
                return $maintenanceMode->handle($request, $next);
            }
    
            return $next($request);
        }
    
    }
    

    In your app/Http/Kernel.php change

    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode'
    

    to

    'App\Http\Middleware\CheckForMaintenanceMode'
    
    0 讨论(0)
提交回复
热议问题