rajeshtva's avatar

cors issue in post request while deploying laravel inside docker

I have a laravel app which is running behind nginx proxy manager. The laravel app is running inside a docker container and nuxt frontend is also running inside another docker container. Laravel is using fortify, sanctum packages for authentication and session validation.

The issue is that when i try to login using a fake user. then all preflight request, get request works perfectly fine. but when i try to make a post request for login, then it seems to work perfectly fine. as response status is 200 as in header. but says that Access Control Allow Origin header is missing, which causes no response to show up.

what i am missing. where is the issue? this seems to be a common problem.

methods to reproduce.

  • try to login with any email id & password, which ideally should throw 422 error along with validation error messages. but instead comes 200 with no script/ cors error

I have already tried.

  • i added following in my laravel nginx deployment configuration which seems to solve post problem but then when user details is fetched using /api/user(default sanctum user). then 401 issue comes. this doesn't seem to be a valid answer. (
    location / {
        try_files $uri $uri/ /index.php?$query_string;

        if ( $request_method = POST ) {          
            add_header 'Access-Control-Allow-Origin' 'https://codalay.in';         
            add_header 'Access-Control-Allow-Credentials' 'true'; 
        }
    }
  • another way i tried is i added following code in api.codalay.in's reverse proxy nginx configuration . then cors error changes
# custom headers required for cors issues for laravel.
add_header Access-Control-Allow-Origin origin;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Expose-Headers Content-Length;
add_header Access-Control-Allow-Headers Range;

and throws

Reason: CORS header 'Access-Control-Allow-Origin' does not match 'https://codalay.in'

I am trying to fix this issue from 10 days. please help

0 likes
1 reply
mashferd's avatar

Hi,

I think I am in the similar situation. I have a nuxt project in a docker container. I am using socialite login via nuxt/auth-next and proxy for axios.

The laravel api is in another docker container. From "nuxt-container" I need to call that api which is in "laravel-api-container".

"nuxt-container": it is a nuxt ssr app. Run behind nginx. Dockrized. Domain: laraecom.com:4430

"laravel-api-container": It is the api. Run behind nginx. Dockrized. Domain: api.laraecom.com

Now the situation is, laraecom.com:4430 is running. But when I am trying to login via auth-next, axios shows "Network error".

This is the gist for nuxt.config.js gist.github.com/codeperl/5ab6d0aafbbf57a6e77349206f7e772e Did you or anyone, find any solution? I am suffering a couple of days with this and can't move forward.

If someone can help me to find out, that'll be really appreciated!

Thanks in advance!

Please or to participate in this conversation.