Nginx+Gunicorn:无法访问没有根位置的Flask应用程序



我正试图通过nginx+Gunicorn部署一个Flask应用程序。我目前允许访问我的Flask应用程序http://kramericaindustries.hopto.org:8050/或http://kramericaindustries.hopto.org/heatmap/.然而,具有/heatmap/的URI的后面的URL显示了一个屏幕,该屏幕只显示";正在加载"而前者正确加载。我相信这与我的nginx.conf文件有关,但我是nginx的新手,并不确定我做错了什么。我相信这与代理指令有关,但我不知道。下面是我的nginx.conf文件,有问题的区域在底部附近。如果您有任何问题或需要更多信息,请告诉我。谢谢

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
map $http_upgrade $connection_upgrade {
default upgrade;
''      close;
}
server {
listen 80;
server_name kramericaindustries.hopto.org;
rewrite ^/rstudio$ $scheme://$http_host/rstudio/ permanent;
location /rstudio/ {
rewrite ^/rstudio/(.*)$ /$1 break;
proxy_pass http://localhost:8787;
proxy_redirect http://localhost:8787/ $scheme://$http_host/rstudio/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
location /heatmap/ {
# rewrite ^/heatmap/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8000;
# proxy_redirect http://127.0.0.1:8000/ $scheme://$http_host/heatmap/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8050;
server_name kramericaindustries.hopto.org;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

您的索引页试图从哪个位置加载脚本和其他源文件?

对于您的:8050侦听器,您直接从根位置提供服务,并且您的索引页面可能正在提取资源,期望没有额外的/heatmap路径。

例如,当从/heatmap提供服务时,以下内容将失败,因为资源URL没有以该路径为前缀:

<script src="/_dash-component-suites/dash_renderer/polyfill@7.v1_8_3m1605058426.8.7.min.js"></script>

这些将转到404,因为这些资源的正确URL现在是/heatmap/_dash-component-suites/…

如果你要硬编码这些,你必须在中添加热图。如果你用Flask/Jinja2模板渲染索引,你可以在你的URL前面加上{{ request.path }},例如:

<script src="{{ request.path }}/_dash-component-suites/dash_renderer/polyfill@7.v1_8_3m1605058426.8.7.min.js"></script>

当从根位置提供时,它将返回/,当从热图路径提供时,将返回/heatmap

OK我终于明白了,这与nginx或Gunicorn无关。上面的nginx.conf是正确的。这与我正在部署的Flask应用程序有关。我实际上使用的是达世币应用程序(一款使用Flask制作的应用程序(,在声明达世币实例时,必须按原样指定URL basename/"默认情况下。这是我需要的线路

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, url_base_pathname='/heatmap/')

最新更新