2

I am coming from background where a website (asp.net) code was configured on IIS server.

I am now working on Flask and do not have clarity on how the following work together: Flask, WSGI, app server and web server.

Flask is just the python app. Flask makes use of the Werkzeug WSGI (web server gateway interface). that knows how to handle the HTTP protocol request/response. App server is either the development server that comes built-in or other servers for example gunicorn. Web server is something like nginx.

I have static files used in the jinja templates {{ url_for('static', filename='style.css') }}. An app server (Like flask dev server or gunicorn) can process this. But how does nginx (web server) resolve static file address?

Also, in the following sample image, when a request comes to the web server (say nginx), then how does it know that this request has to be forwarded to the app server (gunicorn)? For example it maybe a static file url or maybe an endpoint url.

enter image description here

variable
  • 8,262
  • 9
  • 95
  • 215

1 Answers1

2

So, in you example you have file style.css in static folder. For now the static file is serve from flask web server app not from the nginx. The question is how to serve static file in flask from nginx right ?

  1. first, you create a location block for webapp/static in nginx configuration file
location /webapp {
  proxy_pass http://127.0.0.1:6969;
}

location /webapp/static {
  alias /path/to/webapp/static;
}
  1. And set static_url_path for each webapp: app = Flask(name, static_url_path="/webapp")
Darn
  • 126
  • 7
  • What is the meaning of `location /webapp/static`? – variable Jun 29 '21 at 09:15
  • NGINX to reverse proxy HTTP requests to the upstream server (Gunicorn (WSGI server)) location. you can check more [here](https://www.patricksoftwareblog.com/how-to-configure-nginx-for-a-flask-web-application/) – Darn Jul 02 '21 at 07:32