" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper 博主等级

千里之行,始于足下

  • 累计撰写 206 篇文章
  • 累计创建 14 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录
Web

uWSGI + Nginx 部署项目

PySuper
2019-10-25 / 0 评论 / 0 点赞 / 32 阅读 / 0 字
温馨提示:
本文最后更新于2024-05-28,若内容或图片失效,请留言反馈。 所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

使用Django自带的测试服务器时:./manage.py runserver 127.0.0.1:8000

uWSGI

安装

pip install uwsgi    # 安装uwsgi

uwsgi --version    # 检查uwsgi版本

no internal routing support, rebuild with pcre support

==> sudo apt-get install libpcre3 libpcre3-dev

bind(): Address already in use [core/socket.c line 769]

==> ps -aux | grep uwsgi | awk '{print $2}' | xargs kill -9 # 关闭相关进程

配置

[uwsgi]


# 对外提供 http 服务的端口
http = :8000

# the local unix socket file than commnuincate to Nginx 用于和 nginx 进行数据交互的端口
socket = 127.0.0.1:8001

# the base directory (full path) django 程序的主目录
chdir =/home/opadm/mms_webserver/src

# Django's wsgi file
wsgi-file =src/wsgi.py

# maximum number of worker processes
processes = 100

# thread numbers startched in each worker process
threads = 10

# 一个高阶的cheap模式,在启动的时候只会分配n个工作进程,并使用自适应算法启动新的进程
cheaper = 10

# 在经过sec秒的不活跃状态的进程会被销毁(进入了cheap模式),并最少保留cheaper指定的进程数
idle = 3600

# monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9000

# 设置一个请求的超时时间(秒),如果一个请求超过了这个时间,则请求被丢弃
harakiri = 60

# 当一个请求被harakiri杀掉会,会输出一条日志
harakiri-verbose = true

# 开启内存使用情况报告
memory-report = true

# 设置平滑的重启(直到处理完接收到的请求)的长等待时间(秒)
reload-mercy = 10

# 设置工作进程使用虚拟内存超过N MB就回收重启
reload-on-as= 1024

# 自动给进程命名
auto-procname = true

# 为进程指定前缀
procname-prefix-spaced = xc-mms

# 设置工作进程每处理N个进程就会被回收重启
max-requests=500000

# 设置工作进程使用物理内存超过N MB就回收重启
reload-on-rss=100

# 设置socket超时时间,默认4秒
socket-timeout=10

# 限制http请求体的大小(Bytes)
limit-post=4096

# clear environment on exit
vacuum = true

# 不记录request日志,只记录错误日志
disable-logging = true

# 将日志打印到syslog上
log-syslog = true

# 后台运行,并输出日志
daemonize = /home/opadm/log/uwsgi.log

stats=./uwsgi.status
pidfile=./uwsgi.pid

Nginx

安装

# 安装
sudo apt install nginx

# 查看进程
ps -ef | grep nginx

# 后台运行
uwsgi -d --ini uwsgi.ini

配置

  • 创建用户组
# 添加nginx用户组
groupadd -r nginx

# 添加nginx用户
useradd -r -g nginx nginx
  • 进入Nginx配置目录:cd /etc/nginx ==> cd sites-available
  • 创建配置文件:sudo vim site.conf
server {
    listen 80;
    charset utf-8;

    client_max_body_size 75M;

    location /front_end {
        alias /home/zheng/Documents/Project/Work/INFO/front_end/;
    }

    location / {
        uwsgi_pass 0.0.0.0:8000;
        include /etc/nginx/uwsgi_params;
    }
}
  • 超链:sudo ln -s /etc/nginx/sites-available/site.conf /etc/nginx/sites-enabled/site.conf
  • 重新加载配置文件:nginx -s reload
  • 重启:sudo service nginx restart

BUG / ERROR

uWSGI uwsgi_response_write_body_do(): Connection reset by peer

解决办法:禁用Nginx中的缓冲区

在Nginx配置文件中添加uwsgi_max_temp_file_size 0;或者uwsgi_buffering off;

    ...
    location / {
        uwsgi_pass 0.0.0.0:8000;
        uwsgi_max_temp_file_size 0;
        include /etc/nginx/uwsgi_params;
    }
    ...

我认为从nginx到客户端的传输速度要慢于从uwsgi到nginx的传输速度。

而且nginx的缓冲区已满,因此uwsgi等待很多时间,然后才有空间让nginx接收新数据,这使uwsgi的写请求超时。


0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区