Change admin panel API fetch path

default discord avatar
zsazsagabonk9 months ago
2

Hello, I am trying to run my Payload server through an Apache reverse proxy, but I am having trouble with the admin panel. My reverse proxy's location is set to

/foo

(

not

/

). Since serverURL (

example.com

) does not allow for paths, I ended up changing the webpack config.output.publicPath to

/foo/admin/

. With this,

example.com/foo/api/*

works perfectly and the admin panel is accessible at

example.com/foo/admin/

, but it hangs on the loading screen. It appears that when the admin panel tries to fetch users or any content from the API it fails because it tries

example.com/api/*

instead of

example.com/foo/api/*

.



I tried changing routes.api to

example.com/foo/api/*

, but now the actual API is located at

example.com/foo/foo/api/*

, and the admin panel tries to fetch using

example.com/foo/api/*

(which now doesn't exist).



I know I could just make a new virtual host and essentially run the whole payload server on its own subdomain (e.g.

payload.example.com/admin

), but it is not ideal.



That being said, does anyone know of a good way to change the URL that the admin panel uses to contact the API (or maybe just a better way to go about this)? Thanks in advance!



I just found [this](

https://nouance.io/articles/how-to-host-payloadcms-on-a-subdomain-and-subpath-with-nginx

) link from another thread about subdomains and subpaths for nginx. I'm going to revert the webpack config to default and will try changing routes.admin to

/foo/admin

and routes.api to

/foo/api

and then create an Apache rewrite rule to remove /foo.



I have not been able to get any rewrite rules to work as of yet

  • default discord avatar
    notchr9 months ago

    Good morning @bonk! I've never done this on an apache setup, but are you currently stuck on implementing the rewrites specifically?

  • default discord avatar
    zsazsagabonk9 months ago

    Thanks for the response! I was not able to make it work with the rewrites. I was looking at the apache docs in regards to the proxy flags as well, but I'm not that well versed in apache! This is the proxy section of the virtual host conf file:


    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
        Require all granted
    </Proxy>
    
    <Location /foo>
        ProxyPass http://127.0.0.1:8081
        ProxyPassReverse http://127.0.0.1:8081
    </Location>
    
    RewriteEngine on
    RewriteRule ^foo/(.*)$ $1 [QSA,P]


    I tried with and without /foo admin and api routes on the node side too



    I actually think I figured it out. My proxy location was not correct. I didn't even need the rewrite rules. All I did was change the routes in the payload.config to /foo/api and /foo/admin adjusted my apache conf file to be this:

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
        Require all granted
    </Proxy>
    
    ProxyPass /foo/ http://127.0.0.1:8081/foo/
    ProxyPassReverse /foo/ http://127.0.0.1:8081/foo/
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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