Shikher Gupta
Shikher Gupta

Reputation: 3

Linking with static files on a nginx+gunicorn+flask system

I am setting up a flask web-app running on gunicorn with nginx as the back proxy. For the life of me I cant figure out how to link static files in the template. It keeps giving a 404 error when I try to access the linked file from the web-page, the path showing in the address bar as 127.0.0.1/static/styles/main.css which is obviously wrong. Template engine I am using is the default jinja2. Here's the stylesheet code that I am trying to link with(file --- main.html).

<link rel="stylesheet" href="{{ url_for('static', filename='styles/main.css') }}"/>

the folder structure is :

 entry.py
  /templates
    main.html
  /static
    /styles
    main.css

Do I need to make some changes to the nginx conf file or something? Thanks.

Upvotes: 0

Views: 911

Answers (1)

Andrew Kloos
Andrew Kloos

Reputation: 4608

Yes you will. In your server section of the nginx.conf file add something like this...

   # serve static files - each entry is a separate folder
      location ~ ^/(images|js|css|flash|media|static)/  {
      root    /var/www/html/Web;
      expires 30d;
    }

Hope this helps!

Upvotes: 2

Related Questions