How to use Proxy in Docker container for React PHP App?

0

We have a docker container in which both client and server are running.

Client is running on port 80 whereas server is on port 5000 which is Node server. For some APIs, I want to communicate with hostname on port 5000 and rest of the APIs should communicate with port 80.

We tried different methods to add proxy from react side or add rules in ingress but nothing worked in docker.

Can anyone help on this ? Do we need to do any changes in ingress and service of Aks (Kubernetes Cluster) to achieve this ?

#kubernetes #docker #general-discussions

Well, this question is not really about Docker since you don’t even need Docker behind Kubernetes, but can you describe how you tried with ingress and what the problem was? Do you mean you wan’t to use a proxy for internal communication? And what do you mean by “proxy from react side”? Would it be a proxy service inside in the pod that contains react too?

@rimelek We are using React on client side and PHP on server side. Both are on same docker container. We have some API where we want to use the proxy. It means it should show client URL in API call instead of server URL.

We tried doing this from react side by following article which worked fine in cloud and standalone application but failed in docker application so we tried following rule in ingress but it didn’t work.


location /api/access_token {
proxy_redirect off;
proxy_set_header Host $http_host; # <-- make sure this is $http_host
proxy_set_header X-Forwarded-Host $http_host; # <-- make sure you set this
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 32 8k;
proxy_busy_buffers_size 64k;
send_timeout 90;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000;
}

Thanks for the link. I was a PHP developer but I have never used React so I didn’t undertand why PHP and React had their own ports. So if I understand you, you have a working application without containers, and you want to move the whole application with all of its components to one container.

You should run the API (backend?) in one container and React (frontend?) in an other. Then you could use ingress in Kubernetes to forward /api to the PHP container and everything else to the frontend.

This is what I still don’t understand. The “rule” you shared is in fact a part of the NginX configuration file. When you use Kubernetes Ingress, you don’t directly change the NginX configuration. Are you referring to a simple NginX container as Ingress or are you using the Kubernetes Ingress?