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

[亲身实践]如何通过nginx部署使用dwebsocket的django工程

呼延哲
2023-12-01

[亲身实践]django+nginx+dwebsocket+uwsgi的服务器部署

Django1.11,python2.7支持:

  • Gunicorn
  • uWSGI
  • Apache
  • Nginx

部署django的方式有很多种。

所述文章暂时仅仅讲解django1.11+nginx的方式。

该文章主要解决dwebsocket部署方式所产生的问题,如何去解决和处理实现。

服务器发布:

第一种方案nginx+uwsgi:

uwsgi.ini

[uwsgi]

chdir = /home/user1/xxxxxx/project_name

module = project_name.wsgi

#:application

socket = :8000

;master = true

processes = 1

#!!!!!#!!!!!#!!!!!

threads = 5

max-requests = 6000

chmod-socket    = 666

buffer-size    = 65535

logto = projectname.log

pidfile=uwsgi.pid

#!!!!!#!!!!!#!!!!!

ugreen =''

http-timeout = 300

enable-threads = false

#stats = :3031

#stats-http = true

#plugins=python

DJANGO_SETTINGS_MODULE=project_name.settings

#WEBSOCKET_FACTORY_CLASS="dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory"

#!!!!!#!!!!!#!!!!!

http-websockets=true

nginx.conf

upstream echcom {

    server 127.0.0.1:8000;

}

server {

    listen         10000;

    server_name    127.0.0.1:

    charset UTF-8;

    access_log      /var/log/nginx/web_access.log;

    error_log       /var/log/nginx/web_error.log;

    client_max_body_size 500M;

    location / {

        #include uwsgi_params;                   # 通过uwsgi转发请求

        #uwsgi_pass echcom;              # 和上文配置的socket端口/保持一致

        #uwsgi_read_timeout 300;                  # 设置请求超时时间

        #uwsgi_send_timeout 300;                  # 设置请求超时时间

        #uwsgi_connect_timeout 300;

        #proxy_buffer_size 64k;

        #proxy_buffers   32 32k;

        #proxy_busy_buffers_size 128k;

        uwsgi_pass  127.0.0.1:8000;

        include     uwsgi_params;     # the uwsgi_params file you installed;

        proxy_redirect off;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        proxy_buffer_size 64k;

        proxy_buffers   32 32k;

        proxy_busy_buffers_size 128k;

        uwsgi_send_timeout 600;

        uwsgi_connect_timeout 600;

        uwsgi_read_timeout 600;

    }

    location /static {                          # 访问静态资源

        expires 30d;

        autoindex on;

        add_header Cache-Control private;

        alias /home/user1/xxxxxx/project_name/static/;

    }

    location /media {                          # 访问静态资源

        expires 30d;

        autoindex on;

        add_header Cache-Control private;

        alias /home/user1/xxxxxx/project_name/media/;

    }

}

第二种方案nginx+runserver:

nginx.conf

server {
    listen         10000;
    server_name   www.xxx.com:
    charset UTF-8;
    access_log      /home/user1/xxxxxx/project_name/logs/web_access.log;
    error_log      /home/user1/xxxxxx/project_name/logs/web_error.log;

    client_max_body_size 8192M;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_buffer_size 64k;
        proxy_buffers   32 32k;
        proxy_busy_buffers_size 128k;
 #         uwsgi_send_timeout 120s;
 #       uwsgi_connect_timeout 120s;
        uwsgi_read_timeout 120s;
        proxy_connect_timeout 120s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
    }
    location /static {                          # 访问静态资源
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /home/user1/xxxxxx/project_name/static/;
    }
    location /media {                          # 访问静态资源
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /home/user1/xxxxxx/project_name/media/;
    }
}

一键启动脚本

#!/bin/sh
echo "进入xxxx"
sudo cp /home/user1/xxxxxx/project_name/环境-部署发布/nginx.conf /etc/nginx/conf.d/nginx.conf
echo "停止"
sudo sh /home/user1/xxxxxx/project_name/stop.sh
echo "重新启动nginx"
sudo /etc/init.d/nginx restart
/home/user1/xxxxxx/project_name/manage.py runserver 127.0.0.1:8000
echo "启动success"

cd ..
EOF


 

 类似资料: