Hey everyone! I'm somewhat new to Docker and I've been trying to containerize my web apps that were previously running directly on my home server. Here's the deal: my JavaScript needs to call an API on a different server (let's say Server A), but I can't just do that directly due to CORS restrictions. In the past, I solved this by having my JavaScript send a request to a PHP script on my server, which would then curl Server A and return the data back to my JavaScript. However, now that I'm using Docker, I've set up a separate Nginx container for my HTML/CSS/JavaScript, and I've learned that it's best practice to run PHP in its own container as well. This separation is causing me a CORS problem—my AJAX requests can't reach the PHP script because it's no longer on the same host. What advice do you all have for dealing with this situation? I'm trying to avoid switching to Node.js if possible since I'd have to rewrite everything.
1 Answer
You can actually avoid CORS issues by hosting both your PHP files and your static content in a way that Nginx can manage. While Nginx itself doesn't execute PHP, you can configure it to communicate with a PHP-FPM service on another container. This means that from the JavaScript perspective, everything seems to be coming from the same origin, which eliminates CORS problems. When you share resources correctly across containers, you won't hit cross-origin restrictions at all!
Oh, cool! So I keep my PHP files with the HTML/CSS/JS in one container, and then have another container running PHP-FPM? Nginx will then send the requests to PHP-FPM? Any tips on learning how to set this up? I’m a bit lost on PHP-FPM.