Hi
I have an issue with deploying my django project containing WebSocket
my project works fine in local, and when I deploy on the Ubuntu server using this command(for testing)
uvicorn myproj.asgi:application --host 0.0.0.0 --port 8000
the project runs successfully, and when I enter my server IP in the browser, like http://188.245.*.*:8000
everything works fine and WebSocket connects, in terminal I see this log: "WebSocket /ws/chat/" [accepted]
BUT error occurs when I try to enter my_domain and connect to WebSocket,
I wrote a configuration and tested many solutions to config nginx to use my domain name,
but when I use http://domain.com
I see this log in the terminal :
"GET /ws/chat/ HTTP/1.1" 404 Not Found
It is something like that nginx redirects Websoket to HTTP
the page console when I entered the domain:
my nginx config is /etc/nginx/sites-available/myproject
:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
error_log /var/log/nginx/app_error.log debug;
listen 80;
server_name my_domain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/projectdir/static_files/;
}
location /ws/ {
proxy_pass http://0.0.0.0:8000;
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 86400;
proxy_connect_timeout 86400;
proxy_send_timeout 86400;
}
location / {
proxy_pass http://0.0.0.0:8080;
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;
}
}
I used many different configs and nothing works, and I use some AI bots but still have problems.
some of my codes are below:
settings.py:
...
INSTALLED_APPS = [
"daphne",
. . . other installed apps
]
ASGI_APPLICATION = "myproj.asgi.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
. . .
asgi file:
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from messaging.routing import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket":
AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
),
),
})
messaging.routong :
from django.urls import re_path, path
from . import consumers
websocket_urlpatterns = [
re_path(r'^ws/chat/$', consumers.CombinedConsumer.as_asgi()),
]
and consumer is:
class CombinedConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.user = self.scope['user']
await self.channel_layer.group_add("broadcast", self.channel_name)
await self.accept()
. . .other methods . . .
js code is:
var ws_scheme = window.location.protocol === "https:" ? "wss" : "was";
const chatSocket = new WebSocket(
ws_scheme
+ '://'
+ window.location.host
+ '/ws/chat/'
);