最新消息:20210917 已从crifan.com换到crifan.org

[记录]尝试将Nginx请求转到gunicorn运行的Flask

Flask crifan 2319浏览 0评论

之前折腾了很长时间:

[记录]尝试去在CentOS中折腾uwsgi

都无法正常实现:

将Nginx的请求,转到Flask。

所以放弃了uWSGI。

之前已经通过

在虚拟环境中,用

pip install gunicorn

了:

(SIPEvents)  SIPEvents pip install gunicorn
Collecting gunicorn
  Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
    100% || 122kB 89kB/s 
Installing collected packages: gunicorn
Successfully installed gunicorn-19.6.0
(SIPEvents)  SIPEvents

现在接着继续。

(SIPEvents) ➜  SIPEvents gunicorn –help
usage: gunicorn [OPTIONS] [APP_MODULE]
optional arguments:
  -h, –help            show this help message and exit
  -v, –version         show program’s version number and exit
  –proxy-protocol      Enable detect PROXY protocol (PROXY mode). [False]
  –worker-connections INT
                        The maximum number of simultaneous clients. [1000]
  –statsd-host STATSD_ADDR
                        “host:port“ of the statsd server to log to. [None]
  –max-requests-jitter INT
                        The maximum jitter to add to the *max_requests*
                        setting. [0]
  –pythonpath STRING   A comma-separated list of directories to add to the
                        Python path. [None]
  -R, –enable-stdio-inheritance
                        Enable stdio inheritance. [False]
  -k STRING, –worker-class STRING
                        The type of workers to use. [sync]
  –ssl-version SSL_VERSION
                        SSL version to use (see stdlib ssl module’s) [3]
  –suppress-ragged-eofs
                        Suppress ragged EOFs (see stdlib ssl module’s) [True]
  –log-syslog          Send *Gunicorn* logs to syslog. [False]
  –log-syslog-facility SYSLOG_FACILITY
                        Syslog facility name [user]
  –cert-reqs CERT_REQS
                        Whether client certificate is required (see stdlib ssl
                        module’s) [0]
  –preload             Load application code before the worker processes are
                        forked. [False]
  –keep-alive INT      The number of seconds to wait for requests on a Keep-
                        Alive connection. [2]
  –access-logfile FILE
                        The Access log file to write to. [None]
  -g GROUP, –group GROUP
                        Switch worker process to run as this group. [0]
  –graceful-timeout INT
                        Timeout for graceful workers restart. [30]
  –do-handshake-on-connect
                        Whether to perform SSL handshake on socket connect
                        (see stdlib ssl module’s) [False]
  –spew                Install a trace function that spews every line
                        executed by the server. [False]
  -w INT, –workers INT
                        The number of worker processes for handling requests.
                        [1]
  -n STRING, –name STRING
                        A base to use with setproctitle for process naming.
                        [None]
  –no-sendfile         Disables the use of “sendfile()“. [None]
  -p FILE, –pid FILE   A filename to use for the PID file. [None]
  -m INT, –umask INT   A bit mask for the file mode on files written by
                        Gunicorn. [0]
  –worker-tmp-dir DIR  A directory to use for the worker heartbeat temporary
                        file. [None]
  –limit-request-fields INT
                        Limit the number of HTTP headers fields in a request.
                        [100]
  -c CONFIG, –config CONFIG
                        The Gunicorn config file. [None]
  –log-config FILE     The log config file to use. [None]
  –check-config        Check the configuration. [False]
  –statsd-prefix STATSD_PREFIX
                        Prefix to use when emitting statsd metrics (a trailing
                        “.“ is added, []
  –proxy-allow-from PROXY_ALLOW_IPS
                        Front-end’s IPs from which allowed accept proxy
                        requests (comma separate). [127.0.0.1]
  -u USER, –user USER  Switch worker processes to run as this user. [0]
  –forwarded-allow-ips STRING
                        Front-end’s IPs from which allowed to handle set
                        secure headers. [127.0.0.1]
  –threads INT         The number of worker threads for handling requests.
                        [1]
  –max-requests INT    The maximum number of requests a worker will process
                        before restarting. [0]
  –limit-request-line INT
                        The maximum size of HTTP request line in bytes. [4094]
  –access-logformat STRING
                        The access log format. [%(h)s %(l)s %(u)s %(t)s
                        "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"]
  –certfile FILE       SSL certificate file [None]
  –chdir CHDIR         Chdir to specified directory before apps loading.
                        [/usr/share/nginx/html/SIPEvents]
  –paste STRING, –paster STRING
                        Load a PasteDeploy config file. The argument may
                        contain a “#“ [None]
  –error-logfile FILE, –log-file FILE
                        The Error log file to write to. [-]
  –log-level LEVEL     The granularity of Error log outputs. [info]
  –capture-output      Redirect stdout/stderr to Error log. [False]
  –log-syslog-to SYSLOG_ADDR
                        Address to send syslog messages. [udp://localhost:514]
  –log-syslog-prefix SYSLOG_PREFIX
                        Makes Gunicorn use the parameter as program-name in
                        the syslog entries. [None]
  -D, –daemon          Daemonize the Gunicorn process. [False]
  –ciphers CIPHERS     Ciphers to use (see stdlib ssl module’s) [TLSv1]
  -b ADDRESS, –bind ADDRESS
                        The socket to bind. [[‘127.0.0.1:8000’]]
  -e ENV, –env ENV     Set environment variable (key=value). [[]]
  –reload              Restart workers when code changes. [False]
  –limit-request-field_size INT
                        Limit the allowed size of an HTTP request header
                        field. [8190]
  -t INT, –timeout INT
                        Workers silent for more than this many seconds are
                        killed and restarted. [30]
  –ca-certs FILE       CA certificates file [None]
  –settings STRING     The Python path to a Django settings module.
                        (deprecated) [None]
  –keyfile FILE        SSL key file [None]
  –backlog INT         The maximum number of pending connections. [2048]
  –logger-class STRING
                        The logger you want to use to log events in Gunicorn.
                        [gunicorn.glogging.Logger]

[已解决]Flask中用gunicorn和Nginx出错:502 Bad Gateway

后来再去把IP地址,换成域名:

(SIPEvents) ➜  SIPEvents cat /etc/nginx/conf.d/default.conf
server {
    listen 80;
    #server_name 115.29.173.126;
    server_name hd.webonn.com;
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
    location / {
        proxy_pass         http://127.0.0.1:8080/;
        proxy_redirect     off;
        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;
    }

重启nginx:

(SIPEvents) ➜  SIPEvents service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

用gunicorn启动Flask:

(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 app:app
[2016-08-18 18:00:29 +0000] [20930] [INFO] Starting gunicorn 19.6.0
[2016-08-18 18:00:29 +0000] [20930] [INFO] Listening at: http://127.0.0.1:8080 (20930)
[2016-08-18 18:00:29 +0000] [20930] [INFO] Using worker: sync
[2016-08-18 18:00:29 +0000] [20935] [INFO] Booting worker with pid: 20935
[2016-08-18 18:00:29 +0000] [20938] [INFO] Booting worker with pid: 20938
[2016-08-18 18:00:29 +0000] [20939] [INFO] Booting worker with pid: 20939
[2016-08-18 18:00:29 +0000] [20944] [INFO] Booting worker with pid: 20944

然后再去浏览器中访问:

http://hd.webonn.com/

即可正常显示:

中断运行:

^C[2016-08-18 18:00:54 +0000] [20930] [INFO] Handling signal: int
[2016-08-18 18:00:54 +0000] [20944] [INFO] Worker exiting (pid: 20944)
[2016-08-18 18:00:54 +0000] [20935] [INFO] Worker exiting (pid: 20935)
[2016-08-18 18:00:54 +0000] [20938] [INFO] Worker exiting (pid: 20938)
[2016-08-18 18:00:54 +0000] [20939] [INFO] Worker exiting (pid: 20939)
[2016-08-18 18:00:54 +0000] [20930] [INFO] Shutting down: Master

转载请注明:在路上 » [记录]尝试将Nginx请求转到gunicorn运行的Flask

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
99 queries in 0.188 seconds, using 23.39MB memory