1

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

3
  • config your static path for flask. Commented Mar 21, 2018 at 6:33
  • Possible duplicate of Running a flask app with nginx and gunicorn Commented Mar 21, 2018 at 6:34
  • @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. Commented Mar 21, 2018 at 7:16

1 Answer 1

0

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.