2017-02-28 90 views
0

更新2多克爾遠程主機

我配置configure_blueprints從create_app打電話給我的路線上瓶uwsgi 404。我不想把所有的視圖處理器放在uwsgi.py中。它們存儲在單獨的模塊中。

def configure_blueprints(app): 
    from .root import root_bp 
    ... 
    blueprints = [ 
     (root_bp, None), 
     ... 
    ] 
    for bp, endpoint in blueprints: 
     app.register_blueprint(bp, url_prefix=endpoint) 
    return app 


def create_app(config_fn=None): 
    app = Flask(__name__, template_folder='../templates', static_folder='../static') 
    ... 
    configure_blueprints(app) 
    return app 

應用程序/根/ views.py

root_bp = Blueprint('root_bp', __name__) 

@root_bp.route('/') 
def root(): 
    if not current_user.is_authenticated: 
     return redirect('/login/') 
    return render_template('index.html') 

以下爲SIMPLE_SETTINGS=app.config,instance.docker python3 manage.py list_routes

2017-03-01 06:48:11,381 - passlib.registry - DEBUG - registered 'sha512_crypt' handler: <class 'passlib.handlers.sha2_crypt.sha512_crypt'> 
... 
root_bp.root          HEAD,OPTIONS,GET /
... 

輸出這是實施list_routes命令

@manager.command 
def list_routes(): 
    import urllib 
    output = [] 
    for rule in flask.current_app.url_map.iter_rules(): 
     options = {} 
     for arg in rule.arguments: 
      options[arg] = '[{0}]'.format(arg) 
     methods = ','.join(rule.methods) 
     try: 
      url = flask.url_for(rule.endpoint, **options) 
     except BaseException as e: 
      print('Exc={}'.format(e)) 
     line = urllib.parse.unquote('{:50s} {:20s} {}'.format(rule.endpoint, methods, url)) 
     output.append(line) 
    for line in sorted(output): 
     print(line) 

我做n個不理解爲什麼路線應該放置到文件中,並且不能動態配置。如果這不起作用,我該怎麼辦?

更新

uwsgi.ini

[uwsgi] 
env = SIMPLE_SETTINGS=app.config,instance.docker 
callable = app 
wsgi-file = /var/www/app/uwsgi.py 
uid = www-data 
gid = www-data 
socket = /var/www/app/uwsgi.sock 
chmod-socket = 666 
logto = /var/log/uwsgi/app/app.log 
chdir = /var/www/app 
plugin = python3 
master = true 
processes = 1 

/var/www/app/uwsgi.py

from app import create_app 
app = create_app() 

我配置create_app函數內部藍圖。我認爲他們應該在申請開始時提供。我也使用Flask腳本。

SIMPLE_SETTINGS=app.config,instance.docker python3 manage.py shell 

shell啓動時沒有錯誤。

原帖

我研究過的所有相關問題。我無法解決我的問題。我通過遠程主機上的docker-machine部署我的項目。下面是我的配置:

Dockerfile

FROM ubuntu:latest 
RUN locale-gen en_US.UTF-8 
ENV LANG en_US.UTF-8 
ENV LANG en_US.UTF-8 
ENV LC_ALL en_US.UTF-8 

RUN apt-get update && apt-get install -y \ 
    python3 python3-pip git libpq-dev libevent-dev uwsgi-plugin-python3 \ 
    nginx supervisor 

COPY nginx/flask.conf /etc/nginx/sites-available/ 
COPY supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf 
COPY . /var/www/app 

RUN mkdir -p /var/log/nginx/app /var/log/uwsgi/app /var/log/supervisor \ 
    && rm /etc/nginx/sites-enabled/default \ 
    && ln -s /etc/nginx/sites-available/flask.conf /etc/nginx/sites-enabled/flask.conf \ 
    && echo "daemon off;" >> /etc/nginx/nginx.conf \ 
    && pip3 install -r /var/www/app/python_modules \ 
    && chown -R www-data:www-data /var/www/app \ 
    && chown -R www-data:www-data /var/log 

WORKDIR /var/www/app 

CMD ["/usr/bin/supervisord"] 

泊塢窗,compose.yml

version: '2' 
services: 
    db: 
    image: postgres 
    volumes: 
     - moderator-db:/var/lib/postgresql/data 
    redis: 
    image: redis 
    rabbitmq: 
    image: rabbitmq:3.6 
    api: 
    build: . 
    mem_limit: 1000m 
    ports: 
     - "80:80" 
    depends_on: 
     - redis 
     - db 
     - rabbitmq 
volumes: 
    moderator-redis: 
    driver: local 
    moderator-db: 
    driver: local 

supervisord.conf

[supervisord] 
nodaemon=true 

[program:nginx] 
command=/usr/sbin/nginx 

[program:uwsgi] 
command=uwsgi --ini /var/www/app/uwsgi.ini 

瓶的nginx的conf

server { 

    server_name localhost; 
    listen 80 default_server; 

    charset utf-8; 
    sendfile on; 
    client_max_body_size 70M; 
    keepalive_timeout 0; 

    proxy_buffering on; 
    proxy_buffer_size 8k; 
    proxy_buffers 2048 8k; 
    proxy_ignore_client_abort on; 

    location/{ 
     include uwsgi_params; 
     uwsgi_pass unix:///var/www/app/uwsgi.sock; 
    } 

    location /static { 
     root /var/www/app/static/; 
    } 
} 

$搬運工PS

CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS        NAMES 
d9dec354d97e  moderator_api  "/usr/bin/supervisord" 13 seconds ago  Up 9 seconds  0.0.0.0:80->80/tcp     moderator_api_1 
243c48dac303  postgres   "docker-entrypoint..." 3 hours ago   Up 14 seconds  5432/tcp        moderator_db_1 
23901a761ef1  redis    "docker-entrypoint..." 3 hours ago   Up 14 seconds  6379/tcp        moderator_redis_1 
7cc0683bfe18  rabbitmq:3.6  "docker-entrypoint..." 3 hours ago   Up 16 seconds  4369/tcp, 5671-5672/tcp, 25672/tcp moderator_rabbitmq_1 

尾-f /var/log/uwsgi/app/app.log

[pid: 24|app: 0|req: 1/1] 123.23.7.216() {44 vars in 945 bytes} [Tue Feb 28 14:53:57 2017] GET/=> generated 233 bytes in 10 msecs (HTTP/1.1 404) 3 headers in 311 bytes (1 switches on core 0) 

如果您需要任何額外的信息,請讓我知道。我嘗試了不同的配置:nginx-proxy image,gunicorn等。我對遠程主機有同樣的問題。我還缺少什麼?

+0

即時通訊沒有碼頭專家,但如果其確認'200'從碼頭機內檢查'iptables'確保您正在接受流 – Busturdust

+0

請分享文件uwsgi.ini文件的內容太 –

+0

@Busturdust我可以看到我的請求在應用程序日誌文件中,它看起來像請求來到應用程序 – theodor

回答

0

我花了很多時間,無法解決uwsgi套接字的問題。我決定切換到gunicorn HTTP uwsgi Web服務器。我喜歡安裝的簡單。

supervisord.conf

[supervisord] 
nodaemon=true 
environment=SIMPLE_SETTINGS="app.config,instance.docker,instance.prod" 

[program:nginx] 
command=/usr/sbin/nginx 

[program:app] 
command=gunicorn app:app -b 0.0.0.0:8000 --name app --log-level=debug --log-file=- --worker-class gevent 

nginx的flask.conf

server { 

    server_name your-domain; 
    listen 80; 

    charset utf-8; 
    sendfile on; 
    client_max_body_size 70M; 
    keepalive_timeout 0; 

    root /var/www/app/static/; 

    proxy_buffering on; 
    proxy_buffer_size 8k; 
    proxy_buffers 2048 8k; 
    proxy_ignore_client_abort on; 

    location/{ 
     try_files $uri @proxy_to_app; 
    } 

    location @proxy_to_app { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://localhost:8000; 
    } 

} 

最後一個警告。我用了一個燒瓶snippet對碼頭進行了小的修改。

class ReverseProxied(ProxyFix): 
    def __init__(self, app, config, **kwargs): 
     self.config = config 
     super().__init__(app, **kwargs) 

    def __call__(self, environ, start_response): 
     script_name = environ.get('HTTP_X_SCRIPT_NAME', '') 
     if script_name: 
      environ['SCRIPT_NAME'] = script_name 
      path_info = environ['PATH_INFO'] 
      if path_info.startswith(script_name): 
       environ['PATH_INFO'] = path_info[len(script_name):] 

     scheme = environ.get('HTTP_X_SCHEME', '') 
     if scheme: 
      environ['wsgi.url_scheme'] = scheme 
     # fix for docker to proxy server_name 
     server_name = self.config.get('SERVER_NAME') 
     if server_name: 
      environ['HTTP_X_FORWARDED_HOST'] = server_name 
     return super().__call__(environ, start_response) 

... 

app.wsgi_app = ReverseProxied(app.wsgi_app, app.config) 

應用程序/ config.py

SERVER_NAME = 'localhost:8000' 

實例/ prod.py

SERVER_NAME = 'your-domain' 

現在一切工作正常。