gpt4 book ai didi

nginx - 非默认静态位置的 Flask nginx 和静态文件问题

转载 作者:行者123 更新时间:2023-12-03 15:46:54 25 4
gpt4 key购买 nike

我正在使用 nginx(通过 gunicorn)为 Flask 应用程序提供静态文件。

默认静态文件夹中的静态文件工作正常:

<link rel="stylesheet" href="{{ url_for('static', filename='css/fa/font-awesome.min.css') }}" />

但是,对于我只想限制登录用户访问的其他静态文件,我使用的是 Flask 提供的静态文件夹:
app.register_blueprint(application_view)

application_view = Blueprint('application_view', __name__, static_folder='application_static')

在 html 中,我正在调用一个静态文件:
<link rel="stylesheet" href="{{ url_for('application_view.static', filename='css/main.css') }}" />

然后在 application/application_static 我有受限制的静态文件。这在本地 Flask 安装上运行良好,但是当我使用 Nginx 服务来自/static 文件夹的文件部署到生产机器时,我收到“NetworkError: 404 Not Found - website.com/application_static/main.css”。

关于如何配置 Ngix 来处理这个问题的任何想法?

conf.d/mysitename.conf 文件:
upstream app_server_wsgiapp {
server localhost:8000 fail_timeout=0;
}

server {
listen 80;
server_name www.mysitename.com;
rewrite ^(.*) https://$server_name$1 permanent;
}


server {
server_name www.mysitename.com;
listen 443 ssl;
#other ssl config here
access_log /var/log/nginx/www.mysitename.com.access.log;
error_log /var/log/nginx/www.mysitename.com.error.log info;
keepalive_timeout 5;
# nginx serve up static files and never send to the WSGI server
location /static {
autoindex on;
alias /pathtositeonserver/static;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_wsgiapp;
break;
}
}

# this section allows Nginx to reverse proxy for websockets
location /socket.io {
proxy_pass http://app_server_wsgiapp/socket.io;
proxy_redirect off;
proxy_buffering off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}

nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

最佳答案

gunicorn除非您重新加载配置文件,否则旧代码仍在运行。

您要么停止并重新启动 gunicorn ,或发送 HUP 信号给 gunicorn 进程。

关于nginx - 非默认静态位置的 Flask nginx 和静态文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36620709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com