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



我正在尝试使用Nginx和Gunicorn访问Flask应用程序。我基本上按照这里给出的说明进行操作。我的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 --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,则除/plotary/dash/之外,所有URL都可用,在那里我收到404错误。

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

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

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

最新更新