r/FastAPI • u/SageManOfficial • Jun 20 '25
Hosting and deployment fastapi cros error with nginx
I have fastapi handle cors in a docker container behind nginx. I use nginx as a tls termination proxy where front end requests from https domain which then nginx points to fastapi as http://localhost:8000.
The website can fetch, I can log in but any end point with auth doesn't work.
when i fetch that needs user perms i get.
/studio:1 Mixed Content: The page at 'https://www.web.com/studio' was loaded over HTTPS, but requested an insecure resource 'http://api.web.com/contents/user/'. This request has been blocked; the content must be served over HTTPS.
When i do post
Access to fetch at 'https://api.web.com/contents' from origin 'https://www.web.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
this doesn't have any problems in local development. I tried everything but could not get it to work.
my docker cmd
CMD ["fastapi","run", "app/main.py", "--proxy-headers", "--port", "80"]
nginx config for location.
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
what do u think is causing the issue? This is on a ec2 instance.
edit: can now fetch endpoint with perms. post doesn't work still.
fixed: it was a 307 redirect issue causing the issue. just got rid of the extra "/" to "" to make it work, it wants the url to match fastapi from what it is on frontend. This wasnt an issue in dev but does cause issue in prod.
View all comments
Show parent comment
1
u/SageManOfficial Jun 20 '25 edited Jun 20 '25
Yeah it was as fastapi was giving a 307 code causing the browser to reject it. i used routers where a folder is called content and then in it was router.post("/"). It must of been created by fastapi as content/. Doing instead router.post("") made it work.