Kadiem Alqazzaz
Kadiem Alqazzaz

Reputation: 663

Axum is returning CORS error on virtual local host

The frontend is on app.example.test. The backend is on api.example.test. The error I'm getting is CORS error on fetch but getting ok 200 on preflight: enter image description here

Using local host in /etc/hosts to emulate real link:

127.0.0.1 example.test api.example.test app.example.test

Axum CORS config:

let cors = CorsLayer::new()
    .allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE])
    .allow_origin("http://app.example.test".parse::<HeaderValue>().unwrap())
    .allow_headers([CONTENT_TYPE, AUTHORIZATION]);

let app = Router::new()
    .nest("/nodes", nodes::nodes_router(app_state.clone()))
    .nest("/trees", trees::trees_router(app_state.clone()))
    .nest("/webhooks", auth::webhooks_router())
    .with_state(app_state)
    .layer(cors);

Nginx config:

server {
  listen 80;
  listen [::]:80;
  server_name app.example.test;

  location / {
    proxy_pass http://fekr-frontend:5173;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

server {
  listen 80;
  listen [::]:80;
  server_name api.example.test;

  location / {
    proxy_pass http://fekr-backend:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

Docker compose:

  backend:
    container_name: fekr-backend
    build:
      context: ./backend
      dockerfile: Dockerfile
      target: final
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: $DATABASE_URL
      JWKS_URL: $JWKS_URL
      WEBHOOK_SECRET: $WEBHOOK_SECRET
    command: cargo run
    depends_on:
      - db

  frontend:
    container_name: fekr-frontend
    stdin_open: true
    build:
      context: ./frontend
      dockerfile: Dockerfile
    volumes:
      - ./frontend:/app
      - /app/node_modules
    ports:
      - "5173:5173"
    environment:
      - CHOKIDAR_USEPOLLING=true
    command: npm run dev-exposed

Lastly, if I set allow origin to any .allow_origin(Any) it works.

Upvotes: 0

Views: 36

Answers (0)

Related Questions