当前位置: 首页 > 工具软件 > dwebsocket > 使用案例 >

Nginx + uwsgi + django + websocket(dwebsocket)环境部署

冯哲彦
2023-12-01

Nginx + uwsgi + django + websocket(dwebsocket)环境部署

1.确保django项目能正常跑起来,websocket能调通
2.uwsgi配置

[uwsgi]
socket = 127.0.0.1:8000  
home = /data/env/chat  
chdir = /data/wwwroot/django_range/two_chat/chat 
module = chat.wsgi:application 

master = True


processes = 4
vacuum = True
enable-threads = true
daemonize = /data/wwwroot/django_range/my_uwsgi/uwsgi.log 
wsgi-file = chat/wsgi.py
pidfile = /data/wwwroot/django_range/my_uwsgi/uwsgi.pid
static-map = /media=/data/wwwroot/django_range/back/media

3.nginx配置

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''   close;
}
server
                {
	
                listen                   80;
                server_name              devops.xx.com devops.xx.local;
                access_log               /export/servers/nginx/logs/devops.xx.local/devops.xx.local_access.log main;
                error_log                /export/servers/nginx/logs/devops.xx.local/devops.xx.local_error.log warn;
                error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://www.jd.com/error2.aspx;
					charset     utf-8;
	# #########################核心配置就这2行#########################################
      location / {
				include         /export/servers/nginx/conf/uwsgi_params;
				uwsgi_pass      unix:/export/App/DevOPS/Python_20160906/Python_20160906.sock;
        }
    #################################################################################
      location /static/ {
			alias	/export/App/DevOPS/Python_20160906/static/;
		}
	  # websocket的匹配
      location ~ /index/websocket_demo/ {
			include     uwsgi_params;
			uwsgi_pass      127.0.0.1:8000;
			proxy_redirect off;
			proxy_http_version 1.1;
		    proxy_set_header Upgrade $http_upgrade;
		    proxy_set_header Connection "upgrade";
	}
	    location = / {
			rewrite ^ /index/ permanent;
        }
	    location = /favicon.ico {
			rewrite ^ /static/image/favicon.ico permanent;
	}
		location /devops/ {
			rewrite ^/devops/(.*)$ /$1 last;
	}
        location /software/ {
			alias /export/Data/software/;
			allow all;
			autoindex on;
			autoindex_exact_size off;
			autoindex_localtime on; 
        }
}

4.uwsgi支持django后端的websocket(dwebsocket模块)
(1)修改django项目的settings.py,添加一条数据

WEBSOCKET_FACTORY_CLASS = 'dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory'

(2)修改uwsgi的配置文件,添加异步选项

async = 30
ugreen = ''
http-timeout = 300
 类似资料: