2016-11-18 114 views
0

我很難用Flask-Restplus部署Flask應用程序。當我在一臺機器使用部署的Nginx + uwgsi我的應用程序在http://localhost:5000/api如何使用nginx + uwgsi部署flask-restplus應用程序

本地工作(wezkrug)時,但

一切都很正常,我不斷收到來自服務器404級的響應accesing http://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 nginx的配置藍圖。 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; 
    } 
} 

我必須說,當我嘗試訪問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 

謝謝!

+0

爲什麼這個標記爲複製?我不想添加一個前綴,我不明白這與我的問題有什麼關係...... – AlejandroVK

+0

你說你使用Nginx,然後問你是否必須配置Nginx,這看起來像是一個明顯的「是」 。您是否閱讀過[部署文檔](http://flask.pocoo.org/docs/dev/deploying/uwsgi/)?你目前的配置是什麼樣的?見[mcve]。 – davidism

+0

好的,真正的@davidism,我已經添加了我的nginx配置,關於文檔,這是一個遺留應用程序的一部分,我沒有創建自己,我知道它使用uwsgi和nginx,但沒有更多我害怕 – AlejandroVK

回答

0

如在Flask's documentation中所述,您可能希望使用ProxyFix +對您的nginx配置進行一些修改。

在應用程序代碼,您可能需要做一些事情,如:

# 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) 

那麼你的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; 
     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; 
    } 
} 
+0

This最終成爲我初始化藍圖的一個問題,它基本上是在本地工作的,因爲我的初始化例程正在調用藍圖,但由於我使用另一個腳本來初始化uwgsi應用程序,所以藍圖未被正確初始化那裏。感謝@ziirish的迴應 – AlejandroVK