Open stackerito opened 3 years ago
I've been facing the same, problem.
I'm getting the same error. I have a POST request that does seem to be processed but still receives a CORS error in the browser.
I'd suggest to read this post about how Laravel is working with a reverse proxy: https://medium.com/weekly-webtips/laravel-and-cors-behind-a-reverse-proxy-b737566bdd96
If you're using Laravel 9, you should change the $proxies
variable to '*' in your App/Http/Middleware/TrustProxies.php
file
I am using Docker + Nginx to proxy requests between my frontend React project on
localhost
to backend Laravel API onlocalhost:8080
.I try to register a new user from the frontend on
localhost/register
to Laravellocalhost:8080/api/register
The
GET
request tosanctum/csrf-cookie
works and it sets theXSRF-TOKEN
But then I try to send
POST
request to register a user and I get CORS error:And despite this message, the user is registered successfully. But the error still appears because indeed the response does not contain the ‘Access-Control-Allow-Origin’ header.
I'm not sure if something in the middle (nginx, docker) is blocking it, but the
GET
request clearly works because when I try to change theallowed_origins
in mycors.php
file it throws an error.This is my
cors.php
:This is the
OPTIONS
response headers:This is the
OPTIONS
request headers:This is the
POST
response headers:This is the
POST
request headers:In order to fix this CORS error I added the following to my nginx config:
So now my nginx config file looks like this:
So manually telling nginx to send back these headers for the
POST
request work, but why I don't get this issue forGET
andOPTIONS
?