Replies: 5 comments 2 replies
-
|
I think you just need to set up the CORS middleware: https://fastapi.tiangolo.com/tutorial/cors/ |
Beta Was this translation helpful? Give feedback.
-
|
i have the same problem, i have a react app both dev and build work on windows over nginx with the websocket, but on debian, im struggling. i have checked permission, my nginx .conf file look like this for https ( but i tried with proxypass as http://localhost:8000 as well : which works fine on windows but not on Debian, all my endpoints works fine on Debian over Nginx with cors except websockets, i have tried with both "*" and ip addresses with and without ports on CORS origins, but still not able to reach the Fastapi endpoint, however running a test script like this works like python test_websocket.py : but this does not: ,so i must conlude the nginx .conf file must be handled differently on linux than windows, im just a bit clueless my self here, anyone see something |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
|
Adding to @shahabRDZ, beyond the upgrade headers the other thing that bites people is Nginx's default 60s read timeout silently killing long-lived sockets. Bump |
Beta Was this translation helpful? Give feedback.
-
|
I would first separate the problem into two checks: backend websocket vs Nginx proxy. Your FastAPI echo websocket looks OK for a minimal example, so I would test whether Uvicorn can receive websocket connections locally on the VPS before debugging Nginx. From the VPS, try: curl http://127.0.0.1:8888/
Then test the websocket directly against Uvicorn:
python - <<'PY'
import asyncio
import websockets
async def main():
async with websockets.connect("ws://127.0.0.1:8888/ws") as ws:
await ws.send("hello")
print(await ws.recv())
asyncio.run(main())
PY
If this fails, the issue is not Nginx yet. It is the FastAPI/Uvicorn process.
If it works, test through Nginx:
python - <<'PY'
import asyncio
import websockets
async def main():
async with websockets.connect("ws://123.45.67.89:3333/ws") as ws:
await ws.send("hello")
print(await ws.recv())
asyncio.run(main())
PY
For the Nginx config, I would simplify the /ws location like this:
location /ws {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
And make sure this map is defined in the http {} context, not inside the server {} block:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
Then reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
sudo journalctl -u nginx -n 100 --no-pager
A few extra checks:
If the page is opened with https://, the browser websocket URL must use wss://, not ws://.
If the page is opened with http://, then ws:// is fine.
CORS middleware usually does not fix a failed Nginx websocket upgrade by itself.
If there is Cloudflare or another proxy in front, that layer also needs to support websocket upgrades.
So I would first confirm:
1.ws://127.0.0.1:8888/ws works on the VPS.
2.ws://123.45.67.89:3333/ws works through Nginx.
3.The browser uses ws:// or wss:// depending on the page protocol. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Hi guys
Please tell a newbie
I'm trying to run a simple service with WEBSOCKET to make it work through nginx. This is what my files look like. When I run this on a local PC (Windows 10), it works correctly, but on vps (UBUNTU) with nginx there is no way for the connection to be established via WEBSOCKET.
What am I doing wrong, please give me advice. I haven’t been able to solve this for three days now! Thank you in advance
Operating System
Linux
Operating System Details
UBUNTU
FastAPI Version
0.111.0
Pydantic Version
2.7.1
Python Version
3.8.10
Additional Context
The file is located in the templates folder index.html , code:
nginx code:
When I switch to http://123.45.67.89:3333 then the HTML page loads, but in the developer tools (F12 Chrome) I see this error:
Beta Was this translation helpful? Give feedback.
All reactions