如何使用Nginx + uwgsi部署flask-restplus应用程序 [英] How to deploy a flask-restplus app with nginx + uwgsi

查看:90
本文介绍了如何使用Nginx + uwgsi部署flask-restplus应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用Flask-Restplus部署Flask应用.

http://localhost:5000/api

但是,当我使用nginx + uwgsi在机器上部署我的应用程序时,访问 http时,我会不断从服务器获取404响应://example.com/api ...

好像Flask-Restplus正在将swaggerui用于Swagger ...我是否必须在nginx.conf中添加某些内容来实现此目的?原谅我的无知,但我以前没有使用过Nginx的经验

这就是我声明包含API的蓝图的方式

# Configure the Blueprint for API
blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(programs_namespace)
api.add_namespace(students_namespace)
app.register_blueprint(blueprint)

这是我在/etc/ngingx/conf.d/mysite.conf中的nginx配置

server {
    listen       80;
    server_name  _;
    client_max_body_size 400M;

    location / {
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
        try_files $uri @app;
    }

    location /static/ {
        root /home/mysite/mysite/portal/src/portal; 
        access_log off;
        error_page 404 = @app;
        expires 7d;
    }

    location @app {
        include     uwsgi_params;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

# SSL Server
server {
   listen               443 default_server ssl;
   client_max_body_size 400M;
   ssl_certificate      /etc/nginx/conf.d/mysite.crt;
   ssl_certificate_key  /etc/nginx/conf.d/mysite.key;    
   ssl_protocols  SSLv2 SSLv3 TLSv1;
   ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
   ssl_prefer_server_ciphers   on;
   keepalive_timeout    70;

   location / {
       auth_basic "Restricted";
       auth_basic_user_file htpasswd;
       try_files $uri @app;
   }

   location /static/ {
       root /home/mysite/mysite/portal/src/portal;
       access_log off;
       error_page 404 = @app;
       expires 7d;
   }
   location @app {
        include     uwsgi_params;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

我必须说,当我尝试访问 http://example.com/api 时,我不会完全在日志文件中输出.

该应用程序是使用uwgsi通过初始化脚本运行的,您可以在此处查看其内容:

mysite_dir=${mysite_DIR-/home/mysite/mysite/portal/src/portal}
pidfile=${PIDFILE-/var/run/mysite/mysite.pid}
uwsgi_bin=/usr/local/bin/uwsgi
uwsgi_url=127.0.0.1:5000
uwsgi_app=uwsgiapp
uwsgi_parameters="-p 4 -M -t 300"
logfile=${LOGFILE-/var/log/mysite.log}
user=mysite

chk_pidfolder() {
    [ -d "${pidfile%/*}" ] || install -o $user -g $user -d "${pidfile%/*}"
}

start() {
    log_daemon_msg "Starting mysite"
    start-stop-daemon --start -c $user -g $user --pidfile $pidfile --exec $uwsgi_bin -- --chdir $mysite_dir --pidfile $pidfile -s $uwsgi_url -w $uwsgi_app $uwsgi_parameters -d $logfile --callable app --enable-threads --post-buffering 4096
    log_end_msg $?
}   

stop () {
    log_daemon_msg "Stopping mysite"
    if [ -e "$pidfile" ]; then
        pid=`cat "$pidfile"`
    else
        pid=`pidofproc $uwsgi_bin`
    fi
    kill $pid
    stopped=false
    for ((i=0; i<10; i++)); do
        if [ -d /proc/$pid ]; then
            printf "."
            sleep 2
        else
            stopped=true
            break
        fi
    done
    if $stopped ; then
        rm -f $pidfile
        log_end_msg 0
    else
        kill -6 $pid
        log_warning_msg "no success, attempted to kill the process with -6, manual check recommended"
    fi
}

case "$1" in
    start)
        chk_pidfolder
        start
        ;;
    stop)
        stop
        ;;
    status)
        log_daemon_msg "Status of mysite"
        status_of_proc -p $pidfile $uwsgi_bin mysite
  #      if [ $? -eq 0 ]; then
  #        log_end_msg 0
  #        exit 0
  #      else
  #        log_end_msg 1
  #        exit 1
  #      fi
        ;;
    restart)
        log_daemon_msg "Restarting mysite"
        stop
        sleep 4
        start
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart}"
        exit 2
        ;;
esac
exit 0

谢谢!

解决方案

I'm having a hard time deploying a Flask app with Flask-Restplus.

Everything works great when working locally (wezkrug) in http://localhost:5000/api

But when I deploy in a machine my app using nginx + uwgsi, I keep on getting 404 responses from server when accesing http://example.com/api...

Looks like Flask-Restplus is using swaggerui for Swagger...do I have to add something in nginx.conf to serve this? Forgive my ignorance but I have no previous experience with nginx

This is how I declared the Blueprint that contains the API

# Configure the Blueprint for API
blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(programs_namespace)
api.add_namespace(students_namespace)
app.register_blueprint(blueprint)

This is my nginx configuration at /etc/ngingx/conf.d/mysite.conf

server {
    listen       80;
    server_name  _;
    client_max_body_size 400M;

    location / {
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
        try_files $uri @app;
    }

    location /static/ {
        root /home/mysite/mysite/portal/src/portal; 
        access_log off;
        error_page 404 = @app;
        expires 7d;
    }

    location @app {
        include     uwsgi_params;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

# SSL Server
server {
   listen               443 default_server ssl;
   client_max_body_size 400M;
   ssl_certificate      /etc/nginx/conf.d/mysite.crt;
   ssl_certificate_key  /etc/nginx/conf.d/mysite.key;    
   ssl_protocols  SSLv2 SSLv3 TLSv1;
   ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
   ssl_prefer_server_ciphers   on;
   keepalive_timeout    70;

   location / {
       auth_basic "Restricted";
       auth_basic_user_file htpasswd;
       try_files $uri @app;
   }

   location /static/ {
       root /home/mysite/mysite/portal/src/portal;
       access_log off;
       error_page 404 = @app;
       expires 7d;
   }
   location @app {
        include     uwsgi_params;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

I must say that when I try to access http://example.com/api I get no output at log files at all.

The app is run via a init script using uwgsi, you can see the contents here:

mysite_dir=${mysite_DIR-/home/mysite/mysite/portal/src/portal}
pidfile=${PIDFILE-/var/run/mysite/mysite.pid}
uwsgi_bin=/usr/local/bin/uwsgi
uwsgi_url=127.0.0.1:5000
uwsgi_app=uwsgiapp
uwsgi_parameters="-p 4 -M -t 300"
logfile=${LOGFILE-/var/log/mysite.log}
user=mysite

chk_pidfolder() {
    [ -d "${pidfile%/*}" ] || install -o $user -g $user -d "${pidfile%/*}"
}

start() {
    log_daemon_msg "Starting mysite"
    start-stop-daemon --start -c $user -g $user --pidfile $pidfile --exec $uwsgi_bin -- --chdir $mysite_dir --pidfile $pidfile -s $uwsgi_url -w $uwsgi_app $uwsgi_parameters -d $logfile --callable app --enable-threads --post-buffering 4096
    log_end_msg $?
}   

stop () {
    log_daemon_msg "Stopping mysite"
    if [ -e "$pidfile" ]; then
        pid=`cat "$pidfile"`
    else
        pid=`pidofproc $uwsgi_bin`
    fi
    kill $pid
    stopped=false
    for ((i=0; i<10; i++)); do
        if [ -d /proc/$pid ]; then
            printf "."
            sleep 2
        else
            stopped=true
            break
        fi
    done
    if $stopped ; then
        rm -f $pidfile
        log_end_msg 0
    else
        kill -6 $pid
        log_warning_msg "no success, attempted to kill the process with -6, manual check recommended"
    fi
}

case "$1" in
    start)
        chk_pidfolder
        start
        ;;
    stop)
        stop
        ;;
    status)
        log_daemon_msg "Status of mysite"
        status_of_proc -p $pidfile $uwsgi_bin mysite
  #      if [ $? -eq 0 ]; then
  #        log_end_msg 0
  #        exit 0
  #      else
  #        log_end_msg 1
  #        exit 1
  #      fi
        ;;
    restart)
        log_daemon_msg "Restarting mysite"
        stop
        sleep 4
        start
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart}"
        exit 2
        ;;
esac
exit 0

Thanks!

解决方案

As stated in the Flask's documentation, you probably want to use ProxyFix + a few modifications to your nginx configuration.

In your application code, you may want to do something like:

# Configure the Blueprint for API
blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(programs_namespace)
api.add_namespace(students_namespace)
app.register_blueprint(blueprint)

# Here we patch the application
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)

Then your nginx configuration will have to look:

server {
    listen       80;
    server_name  _;
    client_max_body_size 400M;

    location / {
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
        try_files $uri @app;
    }

    location /static/ {
        root /home/mysite/mysite/portal/src/portal; 
        access_log off;
        error_page 404 = @app;
        expires 7d;
    }

    location @app {
        include     uwsgi_params;
        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;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

# SSL Server
server {
   listen               443 default_server ssl;
   client_max_body_size 400M;
   ssl_certificate      /etc/nginx/conf.d/mysite.crt;
   ssl_certificate_key  /etc/nginx/conf.d/mysite.key;    
   ssl_protocols  SSLv2 SSLv3 TLSv1;
   ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
   ssl_prefer_server_ciphers   on;
   keepalive_timeout    70;

   location / {
       auth_basic "Restricted";
       auth_basic_user_file htpasswd;
       try_files $uri @app;
   }

   location /static/ {
       root /home/mysite/mysite/portal/src/portal;
       access_log off;
       error_page 404 = @app;
       expires 7d;
   }
   location @app {
        include     uwsgi_params;
        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;
        uwsgi_pass  127.0.0.1:5000;
        access_log  /var/log/nginx/mysite_access.log main;
        error_log   /var/log/nginx/mysite_error.log warn;
    }
}

这篇关于如何使用Nginx + uwgsi部署flask-restplus应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆