Search code examples
nginxflaskwebservergunicornappserver

How does web server know that it has to send the request to the app server and static files?


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


Solution

  • 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")