admin管理员组

文章数量:1279120

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:

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(";.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.

本文标签: dockerAxum is returning CORS error on virtual local hostStack Overflow