V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
python30
V2EX  ›  Python

django2.2 python3.6 nginx 时不时的出现下面错误,网站很卡,然后就 出现: 502 Bad Gateway 然后自已又变好了。速度快起来

  •  
  •   python30 · 2020-03-21 00:20:46 +08:00 · 3023 次点击
    这是一个创建于 1715 天前的主题,其中的信息可能已经有所发展或是发生改变。
    绑名的域名:
    gq.xxbbbb.com www.xxbbbb.com

    django2.2 python3.6 nginx

    不知道什么原因
    时不时的出现下面错误,网站很卡,然后就 出现:502 Bad Gateway 然后自已又变好了。速度快起来



    日志:
    2020/03/21 00:03:53 [error] 1084#0: *7404 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 66.249.71.49, server: gq.xxbbbb.com, request: "GET /company/13864311189/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:92001", host: "www.xxbbbb.com"
    2020/03/21 00:03:56 [error] 1085#0: *7374 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 157.55.39.81, server: gq.xxbbbb.com, request: "GET /class/info/11329 HTTP/1.1", upstream: "uwsgi://127.0.0.1:92001", host: "www.xxbbbb.com"
    2020/03/21 00:04:07 [error] 1084#0: *7407 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 123.169.102.171, server: gq.xxbbbb.com, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:92001", host: "www.xxbbbb.com", referrer: "https://www.xxbbbb.com/m/tags/xxx/"
    2020/03/21 00:04:20 [error] 1084#0: *7461 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 203.208.60.76, server: gq.xxbbbb.com, request: "GET /class/info/4205 HTTP/1.1", upstream: "uwsgi://127.0.0.1:92001", host: "gq.xxbbbb.com"
    2020/03/21 00:04:25 [error] 1084#0: *7416 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 103.14.35.86, server: gq.xxbbbb.com, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:92001", host: "www.xxbbbb.com", referrer: "https://www.xxbbbb.com/"


    是人数一多了。超时了?
    12 条回复    2020-05-19 17:04:48 +08:00
    a719114136
        1
    a719114136  
       2020-03-21 01:33:22 +08:00 via Android
    从现象来看很可能是并发太高了,一般是 uwsgi 超时了。

    最好改用 gunicorn,uwsgi 配置太复杂了,一堆配置不知道改哪个。
    jenlors
        2
    jenlors  
       2020-03-21 09:33:20 +08:00 via Android
    nginx 配置,系统参数,uwsgi 都可以针对高并发调优。
    676529483
        3
    676529483  
       2020-03-21 09:33:22 +08:00
    502 超时了,看看是不是有接口速度太慢影响了,或者也可以看下是不是带宽满了
    python30
        4
    python30  
    OP
       2020-03-21 11:22:46 +08:00
    确实是人数一多就这样卡住了
    但是配配是 2 核 4G 3M 的
    不应该是带宽问题
    因为 1 分钟最大同时在线才 18 人就卡住了

    下面是我的配置参数,刚从网上看了教程改的
    不知道还有需要优化或有错的地方不?

    centos7.5 python3.6 django2.2



    ps: 在卡住的那个时间点我看了一下日志
    还有少少百度蜘蛛来网站!难道这个也有关系?

    uwsgi.conf
    ```
    # myweb_uwsgi.ini file
    [uwsgi]

    #django-related settings
    socket = 127.0.0.1:8001
    # the base directory (full path)
    chdir = /var/www/website

    # Django s wsgi file
    module = website.wsgi:application

    env = DJANGO_SETTINGS_MODULE = website.settings
    daemonize = /var/log/nginx/website_access.log;
    # process-related settings
    # master
    master = true
    py-autoreload = 1

    # maximum number of worker processes
    processes = 4
    threads = 2
    # ... with appropriate permissions - may be needed
    # chmod-socket = 664
    # clear environment on exit
    vacuum = true
    ```




    nginx.conf

    ```
    user nginx;
    worker_processes 4;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;

    include /usr/share/nginx/modules/*.conf;
    worker_rlimit_nofile 65535;
    events {
    use epoll;
    worker_connections 20000;
    }

    http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 60;
    client_header_buffer_size 4k;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
    listen 443;
    server_name website.com;
    ssl on;
    ssl_certificate 1_www.website.com_bundle.crt;
    ssl_certificate_key 2_www.website.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    charset UTF-8;
    access_log /var/log/nginx/website_access.log;
    error_log /var/log/nginx/website_error.log;

    client_max_body_size 75M;
    fastcgi_buffers 8 128k;
    fastcgi_busy_buffers_size 128k;


    location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:8001;
    uwsgi_send_timeout 300;
    uwsgi_connect_timeout 300;
    uwsgi_read_timeout 300;
    }
    location /static {
    expires 30d;
    autoindex on;
    #return 403;
    add_header Cache-Control private;
    alias /var/wwwr/website/static/;
    }
    location /media {
    expires 30d;
    autoindex on;
    #return 403;
    add_header Cache-Control private;
    alias /var/wwwr/website/media/;
    }
    }
    ```
    lance86
        5
    lance86  
       2020-03-21 11:41:28 +08:00
    没有用过 uwsgi,不过你试试把 uwsgi 配置中的 theads 调大一些?
    奇怪的是你 nginx 上配置的 timeout 是 300s 啊,你也试试在 django 里把把个请求的花费时间给记录一下,主要看下有没有卡住。
    顺便可以在 nginx 的 log 格式中加上$upstream_response_time,作为参考,可以看看是不是卡住之前,response time 都变长了。
    python30
        6
    python30  
    OP
       2020-03-21 12:00:41 +08:00
    @lance86 我把 theads 设成了 4 了 感觉 4-10 都可以。

    上面的是我修改后的配置
    修改后现在网站上没人在线
    也不知道这配置起作用不
    xpresslink
        7
    xpresslink  
       2020-03-21 14:30:04 +08:00
    上 gevent 啊,这种情况虽然不能提高性能,但是能拖住更多连接。
    uwsgi.conf
    加一行

    # 使用 gevet 异步 loop
    gevent=128

    然后$ source venv/bin/activiate;pip install gevent
    hushao
        8
    hushao  
       2020-03-21 21:32:29 +08:00 via iPhone
    会不会是数据库问题?我之前貌似碰到过数据库导致此类问题。只是提供一种参考
    python30
        9
    python30  
    OP
       2020-03-21 21:45:57 +08:00
    @xpresslink
    谢谢
    今天一直没出现这问题
    明天在线人数多的时候出现这问题。我再修改
    python30
        10
    python30  
    OP
       2020-03-21 21:47:00 +08:00
    @hushao
    能详细说说吗?
    还真有可能是这事
    我刚从 mysql5.6 mysqldump 备份
    直接导入到 mysql5.7 里面!
    你当时是怎么解决的?
    python30
        11
    python30  
    OP
       2020-03-23 11:12:55 +08:00
    @hushao 又出现这个问题了
    请问一下,你当时是怎么处理的你的数据库?
    Skyline57
        12
    Skyline57  
       2020-05-19 17:04:48 +08:00
    看看系统占用?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2914 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:36 · PVG 11:36 · LAX 19:36 · JFK 22:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.