How to setup apache2 virtual host with 2 domains (same domain) for SPA application?

I’m developing 2 sites as frontend (http://localhost:2000) and backend site (http://localhost:5000). I try to send cookie from frontend to backend but not success and I know to do this I must set 2 sites is the same domain.
The first thing I want to say is I dont know “the same domain is” and here is what I try. I make a config file like this:

<VirtualHost *:80>
     ProxyPreserveHost On
     ServerName fe.ecom-dev.local
     ProxyPass / http://fe.ecom-dev.local
     ProxyPassReverse / http://fe.ecom-dev.local/
</VirtualHost>
<VirtualHost *:80>
     ProxyPreserveHost On
     ServerName be.ecom-dev.local
     ProxyPass / http://be.ecom-dev.local
     ProxyPassReverse / http://be.ecom-dev.local/
</VirtualHost>

and in the hosts file:

localhost:2000	fe.ecom-dev.local
localhost:5000	be.ecom-dev.local

What did I do are correct?

First, the hosts file doesn’t understand (nor should it) port numbers, it should be changed to just

localhost	fe.ecom-dev.local
localhost	be.ecom-dev.local

As for your cookie problem, you should indicate to the browser that the cookie is for ecom-dev.local, rather than be.ecom-dev.local or fe.ecom-dev.local. How you do this depends on the technology you’re using, but generally there is a parameter called domain when you set a cookie, that should be ecom-dev.local.

For a javascript example, see https://javascript.info/cookie#domain

This is unconvinient, I still use port in new virtual host. I mean I want to setup fe.ecom-dev.local for locahost:2000 and be.ecom-dev.local for localhost:5000, not fe.ecom-dev.local:2000.
Here is new config I found but it does not work:

 <VirtualHost *:80> 
   ProxyPreserveHost On
   ProxyRequests Off
   ServerName www.fe.ecom-dev.local
   ServerAlias fe.ecom-dev.local
   ProxyPass / http://localhost:2000/
   ProxyPassReverse / http://localhost:2000/
 </VirtualHost>

Did you read my post at all? Your answer seems to indicate you did not.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.