uwsgi 和 flask的使用

uwsgi 和 flask

1. 啓動

$ uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app 

*** Starting uWSGI 2.0.11.1 (64bit) on [Thu Aug  6 10:40:43 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 22 July 2015 19:46:14
os: Linux-2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014
nodename: petool001Z
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /home/admin/cdoctor/petapi/src
detected binary path: /opt/gentoo/usr/local/python-2.7.8/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:8002 fd 3
Python version: 2.7.8 (default, Dec  3 2014, 10:14:25)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
Python main interpreter initialized at 0xb783b0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
/opt/gentoo/usr/local/python-2.7.8/lib/python2.7/site-packages/setuptools-18.0.1-py2.7.egg/pkg_resources/__init__.py:1256: UserWarning: /home/admin/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
[2015-08-06 10:40:43,427] [DEBUG] [core.py:223] Initialize the cache
[2015-08-06 10:40:43,427] [DEBUG] [core.py:230] Initialize the simple database pool
[2015-08-06 10:40:43,427] [DEBUG] [core.py:234] Load all the defined handlers/view functions
[2015-08-06 10:40:43,428] [DEBUG] [core.py:181] /home/admin/cdoctor/petapi/src/petapi/api
petapi.api.ecs
ecs <module 'petapi.api.ecs' from './petapi/api/ecs.pyc'>
[2015-08-06 10:40:43,437] [DEBUG] [core.py:237] Application init done, start to serve

錯誤1:html

invalid request block size: 21573 (max 4096)...skip

緣由是:node

鏈接:http://stackoverflow.com/questions/15878176/uwsgi-invalid-request-block-sizepython

uwsgi --http 0.0.0.0:8002 --enable-threads -w manage:app

2. 配合Nginx的使用

步驟:nginx

  • 啓動uwsgi, 這裏要用socketflask

    uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app
  • 修改nginx配置文件api

    server {
    listen 8001;
    server_name xxxxxx;
    
    location / {
            include uwsgi_params;
            uwsgi_pass unix:0.0.0.0:8002;
            }
    }
  • reload nginx多線程

    $ sudo /etc/init.d/nginx reload
    Reloading nginx:                                           [  OK  ]
  • 鏈接測試app

    $ curl -i "http://0.0.0.0:8001/test"
    HTTP/1.1 502 Bad Gateway
    Server: nginx/1.0.15
    Date: Thu, 06 Aug 2015 02:50:27 GMT
    Content-Type: text/html
    Content-Length: 173
    Connection: keep-alive
    
    <html>
    <head><title>502 Bad Gateway</title></head>
    <body bgcolor="white">
    <center><h1>502 Bad Gateway</h1></center>
    <hr><center>nginx/1.0.15</center>
    </body>
    </html>
  • 排查看logpython2.7

    $ sudo tailf /var/log/nginx/error.log
    2015/08/06 10:46:54 [crit] 13609#0: *17 connect() to unix:0.0.0.0:8002 failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: xxxxxx, request: "GET /test HTTP/1.1", upstream: "uwsgi://unix:0.0.0.0:8002:", host: "0.0.0.0:8001"
  • nginx裏面的conf問題寫錯curl

    uwsgi_pass unix:0.0.0.0:8002; # 這個應該去掉unix便可
  • 多線程

    uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app --workers 4 --threads 4
  • 放在後臺執行

    nohup /home/admin/gentoo/usr/local/python-2.7.8/bin/uwsgi --socket 0.0.0.0:8080 --enable-threads -w manage:app --workers 4 --threads 4 &

3. 重啓服務

當修改代碼的時候,必需要重啓服務的,那怎麼辦呢?

相關文章
相關標籤/搜索