1
votes

I'm using flask + gunicorn for my website. and I'm using nginx for reverse proxy only. and set up as nginx config flask proxy setups. I haven't added location /static info in nginx.conf file. so all static files are supposed to be handled by flask itself. but when visiting from public network, it can't load css files. I tried to run flask + gunicorn alone without nginx, it worked well. so why can't flask serve static files by itself behind nginx?

Then I added location /statis info into nginx config file so that nginx may serve static file instead. Still can't load static files. when i looked into error log. it shows

"/var/www/my_project_folder/static/css/style.css" failed (2: No such file or directory), client: 61.152.126.178, server: _, request: "GET /static/css/style.css HTTP/1.1", host: "47.97.209.250", referrer: "http://47.97.209.250"

the path in the error message actually is correct. the last method I tried is to run all script and service under root and chmod my_project to 666. still no success.

I searched on stackoverflow, but no one mentioned the issue that flask itselft can't serve static files behind nginx.

So i hope anyone have any idea or clue about this. and nginx serving issue

Thank

1
config your static path for flask. - Frank AK
@FrankAK, you mean the static path in nginx conf or in flask template? In template i use url_for('static', filename='css/style.css'), which I don't think i need to modify, since i move the whole project to var/www/my_project folder. - X.Z

1 Answers

0
votes

I solved it with: https://www.reddit.com/r/flask/comments/avkjh0/ask_flask_serve_static_files_from_flask_app_via/

nginx.sh:

USE_STATIC_PATH=${STATIC_PATH:-'/app/static'}
location /webappB/static {
  #alias /path/to/webappB/static;
  alias $USE_STATIC_PATH;
}

main.py:

app = Flask(__name__, static_url_path="/webappA")