z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

Access production server slash admin, the resources and form action url get redirected to localhost:4000 #5744

Closed datdang2412 closed 1 year ago

datdang2412 commented 1 year ago

Description:

When uploading laravel admin to production server and accessing domain.com/admin i always get redirected to localhost:400. I checked the admin_url using php artisan tinker and it return the domain.com, not localhost. But still the website continue to serve resource file as localhost.

Steps To Reproduce:

1/ Go to domain.com 2/ Observe missing resource files because all the urls are set to localhost:4000

alexoleynik0 commented 1 year ago

It's very hard to tell.. definitely not the problem of the repo, some server or setup issue it seems like. What happens when you open /admin/auth/login ? Have you cleared routes with php artisan route:clear or cache?

datdang2412 commented 1 year ago

Hi Alex. Yes i tried that. and the problem was setting ADMIN_HTTPS = true. I checked and it seemed the app didn't use APP_URL env variable to operate so i forced it like this:

 /** @var UrlGenerator $url */
        $url = $this->app['url'];

        // Force the application URL
        $url->forceRootUrl(config('app.url'));

It works but there is a problem. Some of the forms generated by resource routes (ajax) have the actions that are generated by this function:

src/Form.php
    /**
     * Get current resource route url.
     *
     * @param int $slice
     *
     * @return string
     */
    public function resource($slice = -2): string
    {
        $segments = explode('/', trim(\request()->getUri(), '/'));

        if ($slice !== 0) {
            $segments = array_slice($segments, 0, $slice);
        }

        return implode('/', $segments);
    }

That function uses request()->getUri() and it returns http://localhost:4000 on the call of domain.com which i believe it's inside a docker container's environment. And it turned out the issue is not coming from laravel-admin.

So all the ajax requests are failed because the action is https://localhost:4000/admin/templates/1 or some kind like that.

datdang2412 commented 1 year ago

Okay. I found the problem, looks like it's a misconfiguration with nginx by using proxy_pass. I have to put

proxy_set_header Host $host;

For the container to serve domain.com as request url.