Мне сложно понять, как обслуживать файлы статистики c для моего Flask веб-приложения через Nginx и Gunicorn. Вся документация, которую я видел в Интернете, указывает на добавление некоторого пути к псевдониму папки stati c, когда клиент делает запрос на файлы stati c. Однако я не уверен, каким должен быть полный путь, поскольку приложение размещено на Heroku. Все запросы к файлам stati c возвращают ошибку 404. Я также заметил, что запрос XHR к файлу JSON, хранящемуся в каталоге /static/json/<file>.json
, не возвращает объект JSON в случае успеха.
Иерархия проекта:
project/
|_ config/
|_ gunicorn.conf.py
|_ nginx.conf.erb
|_ flask_app/
|_ __init__.py
|_ static/
|_ css/
|_ js/
|_ images/
|_ json/
|_ Procfile
|_ run.py
проект / config / gunicorn.conf.py:
def when_ready(server):
open('/tmp/app-initialized', 'w').close()
bind = 'unix:///tmp/nginx.socket'
проект / config / nginx .conf.erb:
daemon off;
#Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections <%= ENV['NGINX_WORKER_CONNECTIONS'] || 1024 %>;
}
http {
server_tokens off;
log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
access_log <%= ENV['NGINX_ACCESS_LOG_PATH'] || 'logs/nginx/access.log' %> l2met;
error_log <%= ENV['NGINX_ERROR_LOG_PATH'] || 'logs/nginx/error.log' %>;
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#Must read the body in 5 seconds.
client_body_timeout 5;
upstream app_server {
server unix:/tmp/nginx.socket fail_timeout=0;
}
server {
listen <%= ENV["PORT"] %>;
server_name _;
keepalive_timeout 5;
# Configure NGINX to deliver static content from the specified folder
location /static {
alias /static;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
}
проект / Procfile
web: bin/start-nginx gunicorn -c config/gunicorn.conf.py 'run:create_app()'
worker: python worker.py
clock: python clock.py
пример. js
export function getData() {
$.ajax({
type: 'GET',
url: '/static/json/data.json',
async: false,
success : function(data) {
currentPageIndex = 0;
numberOfPages = data.length; // Getting undefined error here on "data"
}
});
}