Nginx не удается найти URL-адрес Flask приложения, предоставленного файлом сокета - PullRequest
0 голосов
/ 13 июля 2020

Я пытаюсь получить доступ к приложению Flask, используя Nginx и Gunicorn. Я в основном следовал инструкциям, приведенным здесь . Мое приложение Flask прямо сейчас представляет собой простой Hello World под названием TPD.py:

from flask import Flask

server = Flask(__name__)
@server.route('/')
def hello_world():
    return "Hello World!"
if __name__ == '__main__':
    server.run(debug=True,host='0.0.0.0')

Я могу успешно запустить его, используя python TPD.py. В той же папке у меня есть файл wsgi.py:

from TPD import server

if __name__ == "__main__":
    server.run()

Gunicorn также может без проблем запустить приложение, используя gunicorn --bind 0.0.0.0:5000 wsgi:server. Теперь я использую файл модуля systemd под названием app.service, который выглядит следующим образом:

[Unit]

# specifies metadata and dependencies

Description=Gunicorn instance to serve Dash app
After=network.target

# tells the init system to only start this after the networking target has been reached

# We will give our regular user account ownership of the process since it owns all of the relevant files

[Service]

# Service specify the user and group under which our process will run.
User=stage

# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.

Group=www-data

# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for $

WorkingDirectory=/home/stage/sharOnStoNe/plotary/media/notebooks/
Environment="PATH=/home/stage/sharOnStoNe/plotary/media/notebooks/dash_apps/bin"

# We'll then specify the commanded to start the service

ExecStart=/home/stage/sharOnStoNe/plotary/media/notebooks/dash_apps/bin/gunicorn --workers 3 --bind unix:dash_apps.sock -m 007 wsgi:server

# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-us$

[Install]
WantedBy=multi-user.target

Running sudo systemctl start app и sudo systemctl enable app это создает dash_apps.socks в правильной папке.

Наконец, в /etc/nginx/sites-available/default у меня есть следующие настройки:

server {
    if ($server_port = 8080) {
        rewrite ^/(.*)$ http://$http_host:8081/$1;
    }

    listen 80 default_server;
    listen [::]:80 default_server;

    client_max_body_size    200M;
    location /doku/ {
        alias /var/www/html/;
        include /etc/nginx/mime.types;
        index index.html;
        autoindex on;
    }
    location /plotary/ {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $http_host;
    }
        location /plotary/media/notebooks/ {
                proxy_pass http://10.170.76.24:8888/plotary/media/notebooks/;
        proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # websocket headers
        proxy_set_header Updgrade "websocket";
        proxy_set_header Connection "Upgrade";
    }
    location /plotary/static/ {
        alias /var/www/html/plotary/static/;
                include /etc/nginx/mime.types;
                autoindex on;
    }
        location /plotary/media/ {
                alias /var/www/html/plotary/media/;
                include /etc/nginx/mime.types;
                autoindex on;
        }
    location /plotary/dash/ {
        include proxy_params;
        proxy_pass http://unix:/home/stage/sharOnStoNe/plotary/media/notebooks/dash_apps.sock;
    }

Теперь, если я перезапущу nginx, используя sudo systemctl restart nginx, будут доступны все URL-адреса, кроме /plotary/dash/, где я получаю ошибку 404.

Где я что-то пропустил в этих настройках?

1 Ответ

0 голосов
/ 14 июля 2020

Мне удалось решить проблему, добавив :/ в конец пути к сокету в моей конфигурации nginx. Соответствующий блок местоположения теперь выглядит так:

location /plotary/dash/ {
    include proxy_params;
    proxy_pass http://unix:/home/stage/sharOnStoNe/plotary/media/notebooks/dash_apps.sock:/;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...