CORS Issue in Semi-Production Environment with FastApi + Graphene
Problem Description:
I'm facing an issue with configuring CORS on a FastApi server using Graphene (Starlette). In a semi-production environment, when the server is hosted by uvicorn/gunicorn and accessible from the entire local network (using the --host 0.0.0.0 flag), I don't receive any response other than a CORS error. Even Postman (from the local network via VPN) is unable to fetch the schema from the server.
Previous Attempts to Solve the Issue:
- Setting
allow_orginsto"*"- no effect, and according to the documentation, this option prohibits sending credentials, tokens, etc. - Setting
allow_originsto the computer's address with Postman - no effect. - Setting
allow_originstolocalhostor127.0.0.1- no effect. - Setting
allow_originstohttp://localhostorhttp://127.0.0.1- no effect. - Setting
allow_originstohttp://localhost:80orhttp://127.0.0.1:80- no effect.
Specifications:
Python 3.11
Ubuntu 23.04 Server
Hosting by uvicorn and gunicorn
Framework: FastAPI
Added middleware for CORS handling:
app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:5173"], # Specify the exact source allow_credentials=True, allow_methods=["*", "OPTIONS", "GET", "POST"], allow_headers=["*"], expose_headers=["*"], )GraphQL schema available at
/graphql.
Additional Information:
- Frontend is on the same server as the backend.
- In Postman and the frontend, I provide the server's local IP:8000.
- Port 8000 is open in ufw, and the backend is running on it.
I kindly request assistance in resolving this CORS issue. Thank you in advance for any suggestions and guidance!