*Edit 1: The error seem to be only with POST
requests
I have a frontend website on localhost
.
There is a registration page on localhost/register
The website calls a backend function to register a user at localhost:8080/api/register
I use Axios to POST the username and password.
The browser sends two requests: OPTIONS pre-flight request, and then the POST request.
The user is created successfully, however the browser throws an error for the POST request:
Reason: CORS header ‘Access-Control-Allow-Origin’ missing
And indeed it's missing in the response to the POST.
Assuming my backend cors file is configured properly, could the issue be from the combination of my Docker + Nginx setup that blocks it or proxy the headers to a wrong place?
This is my nginx config:
server {
listen 8080;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
server {
listen 80;
location / {
proxy_pass http://node:3000;
}
}
and this is my docker-compose.yml
:
networks:
mynetwork:
driver: bridge
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:8080"
- "80:80"
volumes:
- ./php:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- node
networks:
- mynetwork
php:
build:
context: ./php
dockerfile: Dockerfile
container_name: php
user: "1000:1000"
volumes:
- ./php:/var/www/html
ports:
- "9000:9000"
networks:
- mynetwork
node:
build:
context: ./react
dockerfile: Dockerfile
container_name: next
volumes:
- ./react:/var/www/html
ports:
- "3000:3000"
networks:
- mynetwork
**Edit 2:
The backend is Laravel and it has a CORS middleware that is supposed to take care of it. And in fact it does seem to be working because GET
and OPTIONS
request pass without error, only the POST
request throws this error.
This is the CORS config file (cors.php
) in Laravel:
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['http://localhost'],
'allowed_origins_patterns' => ['*'],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true