繁体   English   中英

Nginx 找不到由套接字文件提供的 Flask 应用程序的 URL

[英]Nginx can't find URL for Flask app provided by socket file

我正在尝试使用 Nginx 和 Gunicorn 访问 Flask 应用程序。 我基本上按照这里给出的说明进行操作。 我的 Flask 应用程序现在只是一个名为TPD.py的简单 Hello World :

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启动应用程序。 现在我使用一个名为app.service的 systemd 单元文件,如下所示:

[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

运行sudo systemctl start appsudo 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;
    }

现在,如果我使用sudo systemctl restart nginx ,则所有 URL 都可用,除了/plotary/dash/ ,我得到 404 错误。

我在这些设置中错过了什么?

我能够通过在我的 nginx 配置中将:/添加到套接字路径的末尾来解决问题。 相关的位置块现在如下所示:

location /plotary/dash/ {
    include proxy_params;
    proxy_pass http://unix:/home/stage/sharOnStoNe/plotary/media/notebooks/dash_apps.sock:/;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM