Mixed up urls after renaming subdomain

default discord avatar
arctomachine
2 months ago
37

Initially I had

admin.

subdomain. Now I changed it to

cms.

. Updated nginx rules, reissued certificates. Can load dashboard successfully from new subdomain.



However old subdomain is still appearing in some places - in initial message and in media urls (they are trying to load from

admin.

).


With media thumbnails are obviously not working, but also new ones not uploading and old ones not updating (not allowed to perform this action).



Inside main config file server url is loaded from .env file (updated value there to be new subdomain). And inside media collection config static url is also loaded from .env (just

/media

).



What can be wrong here?



"Not allowed to perform this action" is acutally separate issue. It shows this error for any other collection or global



Or perhaps not separate. If it is reading wrong domain, then it might as well be the reason


https://discord.com/channels/967097582721572934/1121089425833726002


I checked multiple times. This variable contains new url. Yet when I build and start server, it says admin panel is available at old url. Does it cache env variables from build to build or something like that?



Well, it looks like that. Starting dev server uses new value, but building and starting prod server uses old value.

  • default discord avatar
    notchr
    2 months ago

    @arctomachine Is it possible that your .env file is not getting updated on prod?



    A lot of times deployments skip .env files



    So it may be reading outdated urls in some cases



    That's my first thought

  • default discord avatar
    arctomachine
    2 months ago

    Is there special argument to force reading .env?

  • default discord avatar
    notchr
    2 months ago

    Well, I would first check the .env file on your server and see what's in it



    Are there any old references?

  • default discord avatar
    arctomachine
    2 months ago

    No, I updated them

  • default discord avatar
    notchr
    2 months ago

    Were there prior to me mentioning it?

  • default discord avatar
    arctomachine
    2 months ago

    I updated them at the time of issuing new certificates for this subdomain, so yes

  • default discord avatar
    notchr
    2 months ago

    ok so both your dev and prod .env files are consistent

  • default discord avatar
    arctomachine
    2 months ago

    It is one file

  • default discord avatar
    notchr
    2 months ago

    what im saying is



    the .env file on your local machine has the same contents as the one on your prod machine?

  • default discord avatar
    arctomachine
    2 months ago

    No

  • default discord avatar
    notchr
    2 months ago

    hmm



    Do you know why?

  • default discord avatar
    arctomachine
    2 months ago

    Because localhost for local and domain name for prod



    Hardcoded value works as expected. So the problem is definitely in reading .env



    Or can it be that old env variable was loaded into memory and never unloaded again, so config reads it from memory instead of file?



    Or is it loading variable from different file? The one that is not .env in root folder?



    I found why it happens. It is pm2 preventing update of variables

  • default discord avatar
    notchr
    2 months ago

    Ah yes



    Happy you figured it out

  • default discord avatar
    arctomachine
    2 months ago

    Only half way. The hard part is to make pm2 respect changes to .env file 😕

  • default discord avatar
    notchr
    2 months ago

    @arctomachine pm2 takes a

    --update-env

    param



    that should refresh your env file

  • default discord avatar
    arctomachine
    2 months ago

    Tried that, not working

  • default discord avatar
    notchr
    2 months ago

    how are you restarting pm2



    when you push to prod



    pm2 restart all



    ?

  • default discord avatar
    arctomachine
    2 months ago

    No, startOrReload --only payload

  • default discord avatar
    notchr
    2 months ago

    hmm



    generally you would have an ecosystem.config.js



    with your app config

  • default discord avatar
    arctomachine
    2 months ago

    Yes, just named differently

  • default discord avatar
    notchr
    2 months ago

    ah



    then



    pm2 restart MYAPP --update-env

  • default discord avatar
    arctomachine
    2 months ago

    That bug was reported in 2014

  • default discord avatar
    notchr
    2 months ago

    can you specify the name of the pm2 app explicitly?

  • default discord avatar
    arctomachine
    2 months ago

    Names are defined in ecosystem file

  • default discord avatar
    notchr
    2 months ago

    i mean, in the restart command



    pm2 restart MYAPPNAME --update-env

  • default discord avatar
    arctomachine
    2 months ago

    Yes, name or id, both work the same

  • default discord avatar
    notchr
    2 months ago

    you've tried that



    ?\

  • default discord avatar
    arctomachine
    2 months ago

    Restart command specifically - no

  • default discord avatar
    notchr
    2 months ago

    I would first try that



    my second thought is if you specify your env vars in your pm2 config rather than an env file



    which seems odd, but is "recommended" by pm2



    for instance



    module.exports = {
      apps : [
          {
            name: "myapp",
            script: "./app.js",
            watch: true,
            env: {
              "NODE_ENV": "development",
            }
          }
      ]
    }


    never tried to do it that way



    But first try the restart command

  • default discord avatar
    arctomachine
    2 months ago

    Tried now, same result

  • default discord avatar
    notchr
    2 months ago

    OK say you add those env vars to the env object in a pm2 config



    Then when you run restart, you pass the changed env var and the flag



    that should tell pm2 to replace the env var



    kind of weird but worth a try



    so you'd end up passing

    ENV_VAR=somethingnew pm2 restart app --update-env


    and specify ENV_VAR in your env object in your pm2 config



    that should update whatever your put as the default in the config file



    @arctomachine Let me know if that works, sorry pm2 can be a bit weird

  • default discord avatar
    arctomachine
    2 months ago

    That worked. And subsequent reset without variable works too now

  • default discord avatar
    notchr
    2 months ago

    Woot! That's good to know



    I'm glad we were able to figure it out

  • default discord avatar
    arctomachine
    2 months ago

    🔥

  • default discord avatar
    notchr
    2 months ago

    Let me know if anything else comes up

  • default discord avatar
    arctomachine
    2 months ago

    The rest should work, just this one was new workflow for me

  • default discord avatar
    notchr
    2 months ago

    PM2 is a nice, at first it was hard for me, but now I use it for all of my projects on deploy



    really nice when you have 2+ things that need restarting

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.