-1

I am new to Nginx and Gunicorn....

I am trying to serve flask app on certain prefix....

ex: https://myweb.com/flask/prefix/

everything works fine except it is not loading static files......

my nginx site configuration looks like below

location /flask/prefix/ {
        include proxy_params;
        proxy_pass http://unix:/home/user/flask_config/flask_socket_file.sock:/;        
    }

when I checked the network section by using Firefox developer tool I found that it is loading home page path / for static files instead of this /flask/prefix....

Example:

/static/image.png (i.e https://myweb.com/static/image.png)

but it suppose to be /flask/prefix/static/image.png (i.e https://myweb.com/flask/prefix/static/image.png).

However I tried to remove :/ at the end of proxy_pass statement... it ended with 501 error....

Please let me know what I am doing wrong....

I followed steps to configure Flask app with Nginx from Here

Praveen
  • 101
  • 2
  • The problem is in the Flask app and not Nginx. I don't know Flask, but you need to set the BaseURL or the static_url_path to point to the correct prefix. – Richard Smith Apr 20 '21 at 06:38

1 Answers1

0

I found solution at last....

sharing here so that it helps someone....

In your wsgi.py file....

from start import app

class PrefixMiddleware(object):

    def __init__(self, app, prefix=''):
        self.app = app
        self.prefix = prefix

    def __call__(self, environ, start_response):

        if environ['PATH_INFO'].startswith(self.prefix):
            environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
            environ['SCRIPT_NAME'] = self.prefix
            return self.app(environ, start_response)
        else:
            start_response('404', [('Content-Type', 'text/plain')])
            return ["This url does not belong to the app.".encode()]

app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/flask/prefix')
if __name__ == "__main__":
    app.run()

with this it should work...

I referred solution from here..

Praveen
  • 101
  • 2