user2653947
user2653947

Reputation: 33

Flask url_for serving images and nginx

I have a router

@app.route('/images/<filename>')
def images(filename):
    send from(os.path.join('uploads', filename))

My images are saved in some directory called 'uploads'. And I configure nginx to serve static files.

My question is when I use url_for('images', filename='1.jpg') in jinja template, which should generate something like src="/images/1.jpg" in browser, if the user click this link, will nginx serve the file or flask serve it?

Another example: when using url_for('static', filename='style.css') in template, is nginx serving it?

Upvotes: 2

Views: 1042

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44092

The solution shall work well, if follows these rules:

  • you know exactly what static files you are going to be served
  • your jinja based templates are generating proper links to these files
  • ngingx is properly configured to serve these static files

Under these (production) conditions, your Python code shall not get requests to these static files as they are served by nginx.

This is desired behaviour, as it off-loads python application by nginx, which can serve those files much more efficiently.

Upvotes: 0

Related Questions