Blaise
Blaise

Reputation: 7518

Python Flask webapp simple proxy configuration to enable SSL/HTTPS with Apache

I have a working webapp based on Python, Flask. (configured to work on http:// host:port)

I need to get it to work with https. I was given an Apache proxy - which redirects all requests in such fashion:

(Apache) https:// host/myApp --> http:// host:port (my Flask based app). Where host:port is the standard configuration where my app was working fine.

I am able to reach the service and index page. However, there is an issue with accessing all static content, which was requested vi url_for method (like ico, images etc.).

Can you point me to any resources/info? Thanks in advance.

Upvotes: 4

Views: 4715

Answers (1)

Rachel Sanders
Rachel Sanders

Reputation: 5884

We add a line in httpd.conf that handles /static/ instead of proxying it to gunicorn:

<VirtualHost oursite.com>

  # Tells apache where /static/ should go
  Alias /static/ /full/path/to/flask/app/static/

  # Proxy everything to gunicorn EXCEPT /static and favicon.ico
  ProxyPass /favicon.ico !
  ProxyPass /static !
  ProxyPass / http://gunicorn.oursite.com:4242/
  ProxyPassReverse / http://gunicorn.oursite.com:4242/

</VirtualHost>

This works because we have gunicorn and apache running on the same box, which may or may not work for you. You might have to copy the static files to the apache host as part of your site deployment.

There's probably a better way to do it, but it works for us.

Upvotes: 4

Related Questions