【问题标题】:Nginx can't find URL for Flask app provided by socket fileNginx 找不到套接字文件提供的 Flask 应用程序的 URL
【发布时间】:2020-07-13 15:51:04
【问题描述】:

我正在尝试使用 Nginx 和 Gunicorn 访问 Flask 应用程序。我基本上遵循了here 给出的说明。我的 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 重新启动 nginx,所有 URL 都可用,除了 /plotary/dash/,我会收到 404 错误。

我在这些设置中遗漏了什么?

【问题讨论】:

  • 我只是猜测:也许应该只有unix:/file/ 没有http://http://IP:port 没有unix:。但是您应该检查 nginx 日志文件 - 可能在 /var/log/nginx
  • 嗨@furas 感谢您的评论!我刚下班。我明天早上第一件事会检查日志。

标签: python nginx flask


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-12-26
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多